版权声明:本文为作者原创文章,可以随意转载,但必须在明确位置标明出处!!!
前两篇文章讲了pandas的基本数据结构Series,DataFrame。还有一种Panel结构没有介绍,Panel因为用的比较少,这里先不对它做介绍,本篇文章主要是介绍一下pandas中的基本函数
add、sub、mul、div
这里最需要理解的是axis(轴)的概念,官方解释:轴用来为超过一维的数组定义的属性,二维数据拥有两个轴:第0轴沿着行方向垂直往下延伸,第1轴沿着列的方向水平延伸。
先看个例子。
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
row = df.iloc[1]
print(df.sub(row, axis=1))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
one three two
a -1.0 NaN -1.0
b 0.0 0.0 0.0
c 1.0 1.0 1.0
d NaN 2.0 2.0
按照定义,轴为1是按照列的方向水平延伸,那么计算逻辑就是1.0 - 2.0 = -1.0;2.0-2.0=0.0;3.0-2.0 = 1.0;NaN - 2.0 = NaN。我们在看看axis=0时的结果:
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
col = df['one']
print(df.sub(col, axis=0))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
one three two
a 0.0 NaN 3.0
b 0.0 6.0 3.0
c 0.0 6.0 3.0
d NaN NaN NaN
按照定义轴为0是安装行的方向垂直向下延伸,所以应该是1.0-1.0= 0.0; NaN - 1.0 =NaN;4.0 - 1.0 = 3.0;可能很多人都会去记axis=1代表的是行,axis=0代表的是列,如果你是这样记的那么下面这种情况你就懵了。
drop、mean
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
print(df.drop('one', axis=1))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
three two
a NaN 4
b 8.0 5
c 9.0 6
d 10.0 7
这里删除一列axis指定的是1,如果你把1记成一行的话那不就只删除了1.0了吗?所以这里的意思是按照one这一列的方向,水平删除每行对应的值,所以如果你要删除某行,你需要这样做df.drop('a', axis=0); 当然mean求平均值也是一个道理。
radd、rsub、rmul、rdiv
add、sub、mul、div、都是用DataFrame中的数据去加、减、乘、除选定的行或列,而radd、rsub、rmul、rdiv与之相反。
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
row = df.iloc[1]
print(df.rsub(row, axis=1))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
one three two
a 1.0 NaN 1.0
b 0.0 0.0 0.0
c -1.0 -1.0 -1.0
d NaN -2.0 -2.0
填充缺省数据
缺省数据的填充可以是用使用函数选项fill_value,也可以使用函数fillna, 使用fill_value选项,如果两个DataFrame数据结构在相同索引位置都为NaN,那么它不会你指定的值去填充
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
print(df.add(df, fill_value=0.0))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
one three two
a 2.0 NaN 8
b 4.0 16.0 10
c 6.0 18.0 12
d NaN 20.0 14
可以从结果中看到NaN依然没有被0.0填充。如果使用fillna就不会出现这种情况
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
print(df.add(df).fillna(0))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
one three two
a 2.0 0.0 8
b 4.0 16.0 10
c 6.0 18.0 12
d 0.0 20.0 14
判断两个DataFrame是否相等使用equals
判断两个DataFrame数据结构是否相等不能用==来判断,因为它是两个对象并不是简单数据类型之间的比较。
import pandas as pd
import numpy as np
data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df = pd.DataFrame(data)
print(df)
print(df + df == df * 2)
print('----------------------')
print((df + df).equals(df * 2))
OUT:
one three two
a 1.0 NaN 4
b 2.0 8.0 5
c 3.0 9.0 6
d NaN 10.0 7
one three two
a True False True
b True True True
c True True True
d False True True
----------------------
True
需要注意的是不管是Series还是DataFrame使用equals函数时它们的index顺序也必须一致才能判断其两个数据结构之间的数值是相等的。
import pandas as pd
import numpy as np
# data = {'one':{'a':1, 'b':2, 'c':3}, 'two':{'a':4, 'b':5, 'c':6, 'd':7}, 'three':{'b':8, 'c':9, 'd':10}}
df1 = pd.DataFrame({'col':['foo', 0, np.nan]})
df2 = pd.DataFrame({'col':[np.nan, 0, 'foo']}, index=[2,1,0])
print(df1.equals(df2))
print(df1.equals(df2.sort_index()))
OUT
False
True
当然pandas还提供了很多统计之列的函数,这里就不一一做出介绍,无论怎么多动手准没错。本章最重要的是要去理解轴的概念。