已信任
Jupyter 服务器: 本地
Python 3: Not Started
[1]
import pandas as pd
import numpy as np
[4]
# 表格函数的自定义
# 将df中所有的元素加2
def add(ele1,ele2):
return ele1+ele2
df = pd.DataFrame(np.random.randn(5,3), columns=['col1','col2','col3'])
df
col1 col2 col3
0 -1.444341 -0.354099 -0.744866
1 0.381602 -0.858716 -0.258652
2 -1.215713 0.058596 0.818322
3 0.750133 1.183576 -0.414067
4 1.929578 1.520465 0.356050
[5]
# 自定义管道函数
df.pipe(add,2)
col1 col2 col3
0 0.555659 1.645901 1.255134
1 2.381602 1.141284 1.741348
2 0.784287 2.058596 2.818322
3 2.750133 3.183576 1.585933
4 3.929578 3.520465 2.356050
[7]
# 按列求平均apply()
df.apply(np.mean)
col1 0.080252
col2 0.309964
col3 -0.048642
dtype: float64
[9]
# 设置轴长,按行
df.apply(np.std, axis=1)
0 0.450998
1 0.506447
2 0.839202
3 0.674594
4 0.666602
dtype: float64
[10]
# 每个元素进行一次操作
df.applymap(lambda x:x*100)
col1 col2 col3
0 -144.434121 -35.409873 -74.486626
1 38.160219 -85.871639 -25.865169
2 -121.571271 5.859575 81.832235
3 75.013306 118.357622 -41.406672
4 192.957780 152.046490 35.604997
[-]