1.apply 和 applymap
1.1 可直接使用NumPy的函数
示例代码:
# Numpy ufunc 函数
df = pd.DataFrame(np.random.randn(5,4) - 1)
print(df)
print(np.abs(df))#绝对值
运行结果:
0 1 2 3
0 -0.062413 0.844813 -1.853721 -1.980717
1 -0.539628 -1.975173 -0.856597 -2.612406
2 -1.277081 -1.088457 -0.152189 0.530325
3 -1.356578 -1.996441 0.368822 -2.211478
4 -0.562777 0.518648 -2.007223 0.059411
0 1 2 3
0 0.062413 0.844813 1.853721 1.980717
1 0.539628 1.975173 0.856597 2.612406
2 1.277081 1.088457 0.152189 0.530325
3 1.356578 1.996441 0.368822 2.211478
4 0.562777 0.518648 2.007223 0.059411
1.2.通过apply将函数应用到列或行上
示例代码:
# 使用apply应用行或列数据
#f = lambda x : x.max()
print(df.apply(lambda x : x.max()))
运行结果:
0 -0.062413
1 0.844813
2 0.368822
3 0.530325
dtype: float64
注意指定轴的方向,默认axis=0,方向是列
示例代码:
# 指定轴方向,axis=1,方向是行
print(df.apply(lambda x : x.max(), axis=1))
运行结果:
0 0.844813
1 -0.539628
2 0.530325
3 0.368822
4 0.518648
dtype: float64
1.3 通过applymap将函数应用到每个数据上
示例代码:
# 使用applymap应用到每个数据
f2 = lambda x : '%.2f' % x#保留两位小数
print(df.applymap(f2))
运行结果:
0 1 2 3
0 -0.06 0.84 -1.85 -1.98
1 -0.54 -1.98 -0.86 -2.61
2 -1.28 -1.09 -0.15 0.53
3 -1.36 -2.00 0.37 -2.21
4 -0.56 0.52 -2.01 0.06
2排序
2.1 索引排序
sort_index()
排序默认使用升序排序,ascending=False 为降序排序
示例代码:
# Series
s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5))
print(s4)
# 索引排序
s4.sort_index() # 0 0 1 3 3
运行结果:
0 10
3 11
1 12
3 13
0 14
dtype: int64
0 10
0 14
1 12
3 11
3 13
dtype: int64
对DataFrame操作时注意轴方向
示例代码:
# DataFrame
df4 = pd.DataFrame(np.random.randn(3, 5),
index=np.random.randint(3, size=3),
columns=np.random.randint(5, size=5))
print(df4)
df4_isort = df4.sort_index(axis=1, ascending=False)
print(df4_isort) # 4 2 1 1 0
运行结果:
1 4 0 1 2
2 -0.416686 -0.161256 0.088802 -0.004294 1.164138
1 -0.671914 0.531256 0.303222 -0.509493 -0.342573
1 1.988321 -0.466987 2.787891 -1.105912 0.889082
4 2 1 1 0
2 -0.161256 1.164138 -0.416686 -0.004294 0.088802
1 0.531256 -0.342573 -0.671914 -0.509493 0.303222
1 -0.466987 0.889082 1.988321 -1.105912 2.787891
2.2 按值排序
sort_values(by='column name') #如果需要按照行进行排序,那就直接使用行名,如果想多列进行排序,可以['a','b']
根据某个唯一的列名进行排序,如果有其他相同列名则报错。
示例代码:
# 按值排序
df4_vsort = df4.sort_values(by=0, ascending=False)
print(df4_vsort)
运行结果:
1 4 0 1 2
1 1.988321 -0.466987 2.787891 -1.105912 0.889082
1 -0.671914 0.531256 0.303222 -0.509493 -0.342573
2 -0.416686 -0.161256 0.088802 -0.004294 1.164138
3唯一值和成员属性
s1 = pd.Series([2,6,8,9,8,3,6],index=['a','a','c','c','c','c','c'])
s1
a 2
a 6
c 8
c 9
c 8
c 3
c 6
dtype: int64
#返回一个series中的唯一值
s2=s1.unique() #返回一个数组
s2
array([2, 6, 8, 9, 3], dtype=int64)
s1.index.is_unique #判断是否为唯一值
False
s1 = pd.Series([2,6,8,9,8,3,6])
s1
0 2
1 6
2 8
3 9
4 8
5 3
6 6
dtype: int64
#计算series值的个数,该函数很实用,也可以指定行列.
s1.value_counts() #返回一个series
6 2
8 2
3 1
2 1
9 1
dtype: int64
#isin 判断值是否存在 返回布尔类型
s1.isin([8]) #判断8是否存在s1
0 False
1 False
2 True
3 False
4 True
5 False
6 False
dtype: bool
#判断多个值
s1.isin([8,2])
0 True
1 False
2 True
3 False
4 True
5 False
6 False
dtype: bool
data = pd.DataFrame({'a':[3,7,9,0],'b':[1,-1,4,8],'c':[0,6,-3,2]})
data
图片.png
data.isin([2,4])
图片.png
4处理缺失数据
示例代码:
df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],
[np.nan, 4., np.nan], [1., 2., 3.]])
print(df_data.head())
运行结果:
0 1 2
0 -0.281885 -0.786572 0.487126
1 1.000000 2.000000 NaN
2 NaN 4.000000 NaN
3 1.000000 2.000000 3.000000
4.1 判断是否存在缺失值:isnull()
示例代码:
# isnull
print(df_data.isnull())
运行结果:
0 1 2
0 False False False
1 False False True
2 True False True
3 False False False
4.2 丢弃缺失数据:dropna()
根据axis轴方向,丢弃包含NaN的行或列。 示例代码:
# dropna
print(df_data.dropna())
print(df_data.dropna(axis=1))
运行结果:
0 1 2
0 -0.281885 -0.786572 0.487126
3 1.000000 2.000000 3.000000
1
0 -0.786572
1 2.000000
2 4.000000
3 2.000000
4.3 填充缺失数据:fillna()
示例代码:
# fillna
print(df_data.fillna(-100.))
运行结果:
0 1 2
0 -0.281885 -0.786572 0.487126
1 1.000000 2.000000 -100.000000
2 -100.000000 4.000000 -100.000000
3 1.000000 2.000000 3.000000
网友评论