对dataframe的行,列加减乘除四则运算
首先,构建一个dataframe
import pandas as pd
d={'one':{'a':1,'b':2,'c':3,'d':4},'two':{'a':5,'b':6,'c':7,'d':8},'three':{'a':9,'b':10,'c':11,'d':12}}
df=pd.DataFrame(d)
print(df)
输出结果为:
one two three
a 1 5 9
b 2 6 10
c 3 7 11
d 4 8 12
以加法为例,对dataframe进行处理
计算每一行的总和
df['sum']=df['one']+df['two']+df['three']
print(df)
输出结果为:
one two three sum
a 1 5 9 15
b 2 6 10 18
c 3 7 11 21
d 4 8 12 24
计算每一列的总和
df.loc['sum']=df.loc['a']+df.loc['b']+df.loc['c']+df.loc['d']
print(df)</