熊猫的函数应用

  

熊猫的函数应用

  
  

应用和applymap

     

1。可直接使用NumPy的函数

  

示例代码:

  
 <=坝镅詐ython代码类> df=pd.DataFrame (np.random。randn (5,4) - 1)
  打印(df)
  
  打印(np.abs (df)  
  

运行结果:

  
 <代码类="语言python "> 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  
  

2。通过将申请函数应用到列或行上

  

示例代码:

  
 <代码类=" language-pyrhon "> #使用应用应用行或列数据
  # f=λx: x.max ()
  打印(df。应用(λx: x.max()))  
  

运行结果:

  
 <代码类="语言python "> -0.062413 0
  1 0.844813
  2 0.368822
  3 0.530325
  dtype: float64  
  
  

注意指定轴的方向,默认轴=0,方向是列

     
 <代码类="语言python "> #指定轴方向,轴=1,方向是行
  打印(df。应用(λx: x.max(),轴=1))
  ”“python
  0 -0.062413
  1 0.844813
  2 0.368822
  3 0.530325
  dtype: float64  
  

3。通过applymap将函数应用到每个数据上

  

示例代码:

  
 <代码类="语言python "> #使用applymap应用到每个数据
  f2=λx:“%。2 f ' % x
  打印(df.applymap (f2)  
  

运行结果:

  
 <代码类="语言python "> 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  
  
  

排序

     

1。索引排序

  
  

sort_index ()

  

排序默认使用升序排序,提升=False为降序排序

     

示例代码:

  
 <代码类="语言python "> #系列
  s4=pd。系列(范围(10、15)指数=np.random。randint(5、大?5))
  打印(s4)
  
  #索引排序
  s4.sort_index() # 0 0 1 3 3  
  

运行结果:

  
 <代码类="语言python "> 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操作时注意轴方向

     

示例代码:

  
 <代码类="语言python "> # DataFrame
  df4=pd.DataFrame (np.random。randn (3、5)
  指数=np.random。randint(3、大?3),
  列=np.random。randint(5、大?5))
  打印(df4)
  
  df4_isort=df4。sort_index(轴=1升=False)
  打印(df4_isort) # 4 2 1 1 0  
  

运行结果:

  
 <代码类="语言python "> 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。按值排序

  
  

sort_values(=傲忻?

  

根据某个唯一的列名进行排序,如果有其他相同列名则报错。

     

示例代码:

  
 <代码类="语言python "> #按值排序
  df4_vsort=df4。sort_values(=0,提升=False)
  打印(df4_vsort)  
  

运行结果:

  
 <代码类="语言python "> 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  
  

处理缺失数据

  

示例代码:

  
 <=坝镅詐ython代码类> df_data=https://www.yisu.com/zixun/pd.DataFrame ([np.random.randn (3), (1。2。,np.nan],
  (np。南4。,np。南]、[1。2。3]])。
  print (df_data.head())  
  

运行结果:

  
 <代码类="语言python "> 0 1 2
  0 -0.281885 -0.786572 0.487126
  1 1.000000 2.000000南
  2南4.000000南
  3 1.000000 2.000000 3.000000 

熊猫的函数应用