深圳定制型网站建设,旅游建设门户网站的方案,谷歌seo收费,曲阜公司网站建设价格Python开发实用教程
DataFrame的运算
DataFrame重载了运算符#xff0c;支持许多的运算
算术运算
运算方法运算说明df.add(other)对应元素的加#xff0c;如果是标量#xff0c;就每个元素加上标量df.radd(other)等效于otherdfdf.sub(other)对应元素相减#xff0c;如果…Python开发实用教程
DataFrame的运算
DataFrame重载了运算符支持许多的运算
算术运算
运算方法运算说明df.add(other)对应元素的加如果是标量就每个元素加上标量df.radd(other)等效于otherdfdf.sub(other)对应元素相减如果是标量就每个元素减去标量df.rsub(other)other-dfdf.mul(other)对应元素相乘如果是标量每个元素乘以标量df.rmul(other)other*dfdf.div(other)对应元素相除如果是标量每个元素除以标量df.rdiv(other)other/dfdf.truediv(other)对应元素相除如果是标量每个元素除以标量df.rtruediv(other)other/dfdf.floordiv(other)对应元素相除取整如果是标量每个元素除以标量df.rfloordiv(other)other//dfdf.mod(other)对应元素相除取余如果是标量每个元素除以标量df.rmod(other)other%dfdf.pow(other)对应元素的次方如果是标量每个元素的other次方df.rpow(other)other**df
import pandas as pddf1 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表格