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 |
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。
运算方法 | 运算说明 |
df.lt(other) | 对应元素进行小于比较,生成一个新的形状相同的DataFrame,如果other是标量,每个元素和这个标量相比较 |
df.gt(other) | 对应元素进行大于比较,生成一个新的形状相同的DataFrame,如果other是标量,每个元素和这个标量相比较 |
df.eq(other) | 判断对应的元素是否相等,生成一个新的形状相同的DataFrame,如果other是标量,每个元素和这个标量相比较 |
df.ne(other) | 判断应元素是否相等,如果不等为True,生成一个新的形状相同的DataFrame,如果other是标量,每个元素和这个标量相比较 |
df.le(other) | 对应元素进行小于等于比较,生成一个新的形状相同的DataFrame,如果other是标量,每个元素和这个标量相比较 |
df.ge(other) | 对应元素进行大于等于比较,生成一个新的形状相同的DataFrame,如果other是标量,每个元素和这个标量相比较 |
df1 = pd.DataFrame({'c1':[1,2,3,4],'c2':[5,6,7,8],'c3':[10,11,12,13]})
print(df1)
‘’‘
c1 c2 c3
0 1 5 10
1 2 6 11
2 3 7 12
3 4 8 13
’‘’
print(df1 == 2) #df1的每个元素是否等于2
‘’‘
c1 c2 c3
0 False False False
1 True False False
2 False False False
3 False False False
’‘’
print(df1%2 == 0) #与计算联合使用,每个元素是否为偶数
‘’‘
c1 c2 c3
0 False False True
1 True True False
2 False False True
3 True True False
’‘’
df2 = pd.DataFrame({'c1':[11,12,13,3],'c2':[6,6,6,6],'c3':[12,11,14,9]})
print(df1 < df2)
'''
c1 c2 c3
0 True True True
1 True False False
2 True False True
3 False False False
'''
其他运算
运算方法 | 运算说明 |
df.abs() | 对每个元素求绝对值 |
DataFrame集合操作
空值操作
运算方法 | 运算说明 |
df.count() | 统计每列的非空值数量 |
df.bfill() | 使用同一列中的下一个有效值填充NaN |
df.ffill() | 使用同一列中的上一个有效值填充NaN |
df.fillna(value) | 使用value填充NaN值 |
df.isna() df.isnull() df.notna() df.notnull() | 检测每个元素是否为NaN,生成一个同形状的DataFrame |
df1 = pd.DataFrame({'c1':[1,2,3,None],'c2':[5,None,None,8],'c3':[10,12,None,16]})
print(df1)
print(df1.bfill())
'''
c1 c2 c3
0 1.0 5.0 10.0
1 2.0 NaN 12.0
2 3.0 NaN NaN
3 NaN 8.0 16.0
c1 c2 c3
0 1.0 5.0 10.0
1 2.0 8.0 12.0
2 3.0 8.0 16.0
3 NaN 8.0 16.0
'''
print(df1.ffill())
'''
c1 c2 c3
0 1.0 5.0 10.0
1 2.0 5.0 12.0
2 3.0 5.0 12.0
3 3.0 8.0 16.0
'''
print(df1.fillna(0))
'''
c1 c2 c3
0 1.0 5.0 10.0
1 2.0 0.0 12.0
2 3.0 0.0 0.0
3 0.0 8.0 16.0
'''
运算方法 | 运算说明 |
df.all() | 返回一个Series,列出每个列的元素是否全部为真,一个列一个结果 |
df.any() | 返回一个Series,列出每个列的元素是否任意一个为真,一个列一个结果 |
df.clip(low,upper) | 返回一个Series,对每个元素与low和upper进行比较,如果小于low,使用low代替,如果大于upper,使用upper代替(可以理解为将超出范围的值设置为边界值) |
df.eval(expr) | 在列上执行表达式 |
df.max() | 列出每列的最大值 |
df.min() | 列出每列的最小值 |
df.mean() | 列出每列的最平均值 |
df.median() | 列出每列的中值 |
df.sum() | 列出每列的元素和 |
df.std() | 列出每列的标准差 |
df.var() | 列出每列的方差 |
df.head(n) | 列出前h行 |
df.tail(n) | 列出后n行 |
df.replace(to_replace,value) | 使用value替换to_repalace的元素,生成一个同形状的新DataFrame |
df.sort_value(by) | 按by指定的列进行排序,可以指定多列 |
df1 = pd.DataFrame({'c1':[1,2,3,4],'c2':[5,None,None,8],'c3':[10,12,None,16]})
print('df1.count():\n', df1.count())
print('df1.max():\n', df1.max())
print('df1.min():\n', df1.min())
print('df1.mean():\n', df1.mean())
print('df1.mean(axis=1):\n', df1.mean(axis=1))
print('df1.sum():\n', df1.sum())
print('df1.std():\n', df1.std())
print('df1.var():\n', df1.var())
'''
df1.count():
c1 4
c2 2
c3 3
dtype: int64
df1.max():
c1 4.0
c2 8.0
c3 16.0
dtype: float64
df1.min():
c1 1.0
c2 5.0
c3 10.0
dtype: float64
df1.mean():
c1 2.500000
c2 6.500000
c3 12.666667
dtype: float64
df1.mean(axis=1):
0 5.333333
1 7.000000
2 3.000000
3 9.333333
dtype: float64
df1.sum():
c1 10.0
c2 13.0
c3 38.0
dtype: float64
df1.std():
c1 1.290994
c2 2.121320
c3 3.055050
dtype: float64
df1.var():
c1 1.666667
c2 4.500000
c3 9.333333
dtype: float64
'''
df1 = pd.DataFrame({'c1':[1,20,30,12],'c2':[50,45,55,38],'c3':[10,10,9,9]})
print(df1)
print(df1.sort_values(by=['c3', 'c1']))
'''
c1 c2 c3
0 1 50 10
1 20 45 10
2 30 55 9
3 12 38 9
c1 c2 c3
3 12 38 9
2 30 55 9
0 1 50 10
1 20 45 10
'''
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表格 |