Divide columns in a DataFrame by a Series (result is only NaNs?)

孤者浪人 提交于 2019-12-20 04:12:40

问题


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

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.

So I've got:

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

and

x
11
12
13

and I'm looking to return:

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})

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

df.mul(df_1.iloc[0])

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


回答1:


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]])

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


来源:https://stackoverflow.com/questions/56692500/divide-columns-in-a-dataframe-by-a-series-result-is-only-nans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!