且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将DataFrame中的列除以序列(结果仅是NaNs?)

更新时间:2021-08-04 23:39:47

NaN的原因是由于索引未对齐.为了克服这个问题,您将需要除以numpy数组,

The cause of NaNs are due to misalignment of your indexes. To get over this, you will either need to divide by numpy arrays,

# <=0.23
df.values / df2[['x']].values  # or df2.values assuming there's only 1 column
# 0.24+
df.to_numpy() / df[['x']].to_numpy()

array([[0.09090909, 0.18181818, 0.27272727],
       [0.33333333, 0.41666667, 0.5       ],
       [0.53846154, 0.61538462, 0.69230769]])

或使用.div执行轴对齐的除法:

Or perform an axis aligned division using .div:

df.div(df2['x'], axis=0)
          a         b         c
0  0.090909  0.181818  0.272727
1  0.333333  0.416667  0.500000
2  0.538462  0.615385  0.692308