将DataFrame中的列除以序列(结果仅是NaNs?) [英] Divide columns in a DataFrame by a Series (result is only NaNs?)

查看:571
本文介绍了将DataFrame中的列除以序列(结果仅是NaNs?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行与此问题中发布的内容类似的操作:

I'm trying to do a similar thing to what is posted in this question: Python Pandas - n X m DataFrame multiplied by 1 X m Dataframe

我有一个具有所有非零浮点值的nxm DataFrame和一个具有所有非零浮点值的1 xm列,并且我试图将nxm数据帧中的每一列除以该列中的值.

I have an n x m DataFrame, with all non-zero float values, and a 1 x m column, with all non-zero float values, and I'm trying to divide each column in the n x m dataframe by the values in the column.

所以我得到了:

a      b      c
1      2      3
4      5      6
7      8      9

x
11
12
13

我希望返回:

a      b     c
1/11   2/11  3/11
4/12   5/12  6/12
7/13   8/13  9/13

我首先尝试了乘法运算,以查看是否可以使用它,因此我尝试应用以上问题答案中给出的两个解决方案.

I've tried a multiplication operation first, to see if I can make it work, so I tried applying the two solutions given in the answer to the question above.

df_prod = pd.DataFrame({c:df[c]* df_1[c].ix[0] for c in df.columns})

这将产生密钥错误0" 并使用其他解决方案:

This produces a "Key Error 0" And using the other solution to :

df.mul(df_1.iloc[0])

尽管形状正确,但这只是给我所有的NaN.

This just gives me all NaN, although in the right shape.

推荐答案

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

这篇关于将DataFrame中的列除以序列(结果仅是NaNs?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆