Python的Pandas库(二)进阶使用

Python开发实用教程

DataFrame的运算

DataFrame重载了运算符,支持许多的运算

算术运算

|---------------------|----------------------------|
| 运算方法 | 运算说明 |
| df.add(other) | 对应元素的加,如果是标量,就每个元素加上标量 |
| df.radd(other) | 等效于other+df |
| df.sub(other) | 对应元素相减,如果是标量,就每个元素减去标量 |
| df.rsub(other) | other-df |
| df.mul(other) | 对应元素相乘,如果是标量,每个元素乘以标量 |
| df.rmul(other) | other*df |
| df.div(other) | 对应元素相除,如果是标量,每个元素除以标量 |
| df.rdiv(other) | other/df |
| df.truediv(other) | 对应元素相除,如果是标量,每个元素除以标量 |
| df.rtruediv(other) | other/df |
| df.floordiv(other) | 对应元素相除取整,如果是标量,每个元素除以标量 |
| df.rfloordiv(other) | other//df |
| df.mod(other) | 对应元素相除取余,如果是标量,每个元素除以标量 |
| df.rmod(other) | other%df |
| df.pow(other) | 对应元素的次方,如果是标量,每个元素的other次方 |
| df.rpow(other) | other**df |

python 复制代码
import pandas as pd

df1 = pd.DataFrame({'c1':[1,2,3,4],'c2':[5,6,7,8],'c3':[10,11,12,13]})
df2 = pd.DataFrame({'c1':[11,12,13,14],'c2':[10,20,30,40],'c3':[100,200,300,400]})
df3 = df1 + df2
print(df3)
'''
   c1  c2   c3
0  12  15  110
1  14  26  211
2  16  37  312
3  18  48  413
'''

df4 = pd.DataFrame({'c1':[11,12,13,14]})
df5 = df1 + df4
print(df5)
'''
   c1  c2  c3
0  12 NaN NaN
1  14 NaN NaN
2  16 NaN NaN
3  18 NaN NaN
'''

df6 = df1 + 1
print(df6)
'''
   c1  c2  c3
0   2   6  11
1   3   7  12
2   4   8  13
3   5   9  14
'''

df7 = df1 -2
print(df7)
'''
   c1  c2  c3
0  -1   3   8
1   0   4   9
2   1   5  10
3   2   6  11
'''

df8 = 2 - df1
print(df8)
'''
   c1  c2  c3
0   1  -3  -8
1   0  -4  -9
2  -1  -5 -10
3  -2  -6 -11
'''

比较运算

比较运算如果是标量,就是每个元素与标量的比较,如果是两个形状一样的DataFrame,生成一个每个元素对应比较的DataFrame。

DataFrame读写文件

|-----------------------------------------------------|----------------------------------|
| 方法名 | 说明 |
| read_table(filepath_or_buffer, *, sep, ...) | 从带分隔符的文件读取 |
| read_csv(filepath_or_buffer, *, sep, ...) | 读csv格式文件 |
| DataFrame.to_csv(path_or_buf, sep, na_rep, ...) | 写csv格式文件 |
| read_fwf(filepath_or_buffer, *, colspecs, ...) | 读固定宽度的格式文件 |
| read_excel(io, sheet_name, header, names, ...) | 读excel文件 |
| DataFrame.to_excel(excel_writer, ...) | 写excel文件 |
| ExcelFile(path_or_buffer, engine, ...) | 用于将表格格式Excel工作表解析为DataFrame对象的类。 |
| ExcelFile.parse(sheet_name, header, names, ...) | 解析一个指定的sheet |
| Styler.to_excel(excel_writer, sheet_name, ...) | 写指定的sheet |
| ExcelWriter(path, engine, date_format, ...) | 用于写入Excel的类 |
| read_json(path_or_buf, *, orient, typ, ...) | 从JSON格式读取数据 |
| DataFrame.to_json(path_or_buf, orient, ...) | 转为为JSON对象字符串 |
| read_html(io, *, match, flavor, header, ...) | 从HTML表格读取数据 |
| DataFrame.to_html(buf, columns, col_space, ...) | 生成HTML表格 |
| Styler.to_html(buf, table_uuid, ...) | 生成HTML表格 |

相关推荐
懂点Tech5 小时前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
comedate6 小时前
SMLib 的 Examples 导读:PyAPI / SM_API / SMLib 三组示例怎么读
python·cad·smlib·examples·sm_api
云和数据.ChenGuang6 小时前
Python 重写(Override)深度解析:从 MRO 到 super() 的完整机制
java·开发语言·人工智能·python·langchain·1024程序员节
川石课堂软件测试6 小时前
学习软件测试需掌握的一些知识
数据库·python·sql·功能测试·网络协议·mysql·http
HRaitest7 小时前
秋招技术岗招聘提效:三类 AI 招聘方案的技术底层与选型逻辑
大数据·人工智能·求职招聘·ai-native
万化冥合7 小时前
PyBox屏幕键盘如何辅助演示键盘操作?
python·计算机外设
车软学堂7 小时前
从测试转开发,到底要补什么
网络·人工智能·python·功能测试·单片机·车载系统·自动化
天空依然蔚蓝7 小时前
分享一个自动检测网站在线状态的python程序
python·运维开发·1024程序员节
岁月宁静7 小时前
一个 AI 项目从本地到上线,要经过哪些步骤?全流程讲清楚
python·docker·agent
codists8 小时前
《crafting interpreters》阅读笔记:p45-p45
python