DataFrame、Series运算
初始配置
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
# 下面两个方法都可以
frame = DataFrame(np.arange(9).reshape(3,3), columns=['a','b','c'], index=['one', 'two', 'threee'])
# print(frame)
series = frame['b']
# print(series)
查看数据
In[3]: frame
Out[3]:
a b c
one 0 1 2
two 3 4 5
threee 6 7 8
In[4]: series
Out[4]:
one 1
two 4
threee 7
Name: b, dtype: int32
DataFrame与Series运算
In[5]: frame.add(series, axis=0) #每列都加series
Out[5]:
a b c
one 1 2 3
two 7 8 9
threee 13 14 15
In[6]: frame.sub(series, axis=0)
Out[6]:
a b c
one -1 0 1
two -1 0 1
threee -1 0 1
In[7]: frame.mul(series, axis=0)
Out[7]:
a b c
one 0 1 2
two 12 16 20
threee 42 49 56
In[8]: frame.div(series, axis=0)
Out[8]:
a b c
one 0.000000 1.0 2.000000
two 0.750000 1.0 1.250000
threee 0.857143 1.0 1.142857
Series之间的运算(最初的frame)
s1 = frame['b'] #1,4,7
s2 = frame['c'] #2,5,8
s1.add(s2) #加
s1.sub(s2) #减
s1.mul(s2) #乘
s1.div(s2) #除
Series自身
#求中位数
s.median()
#求和
s.sum()
#求最大值
s.max()
#求最小值
s.min()