熊猫对齐运算

  

熊猫的对齐运算

  

是数据清洗的重要过程,可以按索引对齐进行运算,如果没对齐的位置则补南最后也可以填充南

  
  

系列的对齐运算

     

1。系列按行,索引对齐

  

示例代码:

  
 <代码类="语言python "> s1=pd。系列(范围(10、20)指数=范围(10))
  s2=pd。系列(范围(20、25)指数=范围(5))
  
  print (s1:)
  打印(s1)  
  

运行结果:

  
 <代码类="语言python "> s1:
  0 10
  1 11
  2 12
  3 13
  4 14
  5 15
  6日16
  7日17
  8日18
  9日19
  dtype: int64
  
  s2:
  0 20
  1 21
  2 22
  3 23
  4 24
  dtype: int64  
  

2。系列的对齐运算

  

示例代码:

  
 <代码类="语言python "> s1 + s2  
  

运行结果:

  
 <代码类="语言python "> 30.0 0
  1 32.0
  2 34.0
  3 36.0
  4 38.0
  5南
  6南
  7南
  8南
  9日南
  dtype: float64  
  
  

DataFrame的对齐运算

     

1。DataFrame按行、列索引对齐

  

示例代码:

  
 <=坝镅詐ython代码类> df1=pd.DataFrame (np.ones((2, 2)),列=[a, b])
  df2=pd.DataFrame (np.ones((3、3)),列=[' a ', ' b ', ' c '])
  
  print (' df1: ')
  打印(df1)
  
  print ()
  print (' df2: ')
  打印(df2)  
  

运行结果:

  
 <代码类="语言python "> df1:
  一个b
  0 1.0 1.0
  1)1.0 - 1.0
  
  df2:
  a b c
  0 1.0 1.0 1.0
  1 1.0 1.0 1.0
  2 1.0 1.0 1.0  
  

2。DataFrame的对齐运算

  

示例代码:

  
 <代码类="语言python "> df1 + df2  
  

运行结果:

  
 <代码类="语言python "> a b c
  0 2.0 2.0南
  1 2.0 2.0南
  2南南南 
  
  

填充未对齐的数据进行运算

     
      <李> fill_value李   
  
  

使用添加、子,div, mul 的同时,

  

通过 fill_value 指定填充值,未对齐的数据将和填充值做运算

     

示例代码:

  
 <代码类="语言python ">打印(s1)
  打印(s2)
  s1。添加(s2, fill_value=https://www.yisu.com/zixun/-1)
  
  打印(df1)
  打印(df2)
  df1。子(df2 fill_value=2)  
  

运行结果:

  
 <代码类="语言python ">打印(s1)
  打印(s2)
  s1。添加(s2, fill_value=https://www.yisu.com/zixun/-1)
  
  打印(df1)
  打印(df2)
  df1。子(df2 fill_value=2)  
  

运行结果:

  
 <代码类="语言python "> #打印(s1)
  0 10
  1 11
  2 12
  3 13
  4 14
  5 15
  6日16
  7日17
  8日18
  9日19
  dtype: int64
  
  #打印(s2)
  0 20
  1 21
  2 22
  3 23
  4 24
  dtype: int64
  
  # s1。添加(s2, fill_value=https://www.yisu.com/zixun/-1)
  0 30.0
  1 32.0
  2 34.0
  3 36.0
  4 38.0
  5 14.0
  6 15.0
  7 16.0
  8 17.0
  9 18.0
  dtype: float64
  
  #打印(df1)
  一个b
  0 1.0 1.0
  1)1.0 - 1.0
  
  #打印(df2)
  a b c
  0 1.0 1.0 1.0
  1 1.0 1.0 1.0
  2 1.0 1.0 1.0
  
  # df1。子(df2 fill_value=2)。
  a b c
  0 0.0 0.0 1.0
  1 0.0 0.0 1.0
  2 1.0 1.0 1.0  

熊猫对齐运算