Python:将DataFrame的每一行除以另一个DataFrame向量 [英] Python: Divide each row of a DataFrame by another DataFrame vector

查看:652
本文介绍了Python:将DataFrame的每一行除以另一个DataFrame向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尺寸为2000 rows x 500 columns(不包括索引)的DataFrame(df1),我想将其每一行除以另一个尺寸为1 rows X 500 columns的DataFrame(df2).两者都有相同的列标题.我试过了:

I have a DataFrame (df1) with a dimension 2000 rows x 500 columns (excluding the index) for which I want to divide each row by another DataFrame (df2) with dimension 1 rows X 500 columns. Both have the same column headers. I tried:

df.divide(df2)df.divide(df2, axis='index')和其他多个解决方案,我总是在每个单元格中获得一个nan值的df.我在函数df.divide中缺少什么参数?

df.divide(df2) and df.divide(df2, axis='index') and multiple other solutions and I always get a df with nan values in every cell. What argument am I missing in the function df.divide?

推荐答案

df.divide(df2, axis='index')中,您需要提供df2的轴/行(例如df2.iloc[0]).

In df.divide(df2, axis='index'), you need to provide the axis/row of df2 (ex. df2.iloc[0]).

import pandas as pd

data1 = {"a":[1.,3.,5.,2.],
         "b":[4.,8.,3.,7.],
         "c":[5.,45.,67.,34]}
data2 = {"a":[4.],
         "b":[2.],
         "c":[11.]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2) 

df1.div(df2.iloc[0], axis='columns')

或者您可以使用df1/df2.values[0,:]

这篇关于Python:将DataFrame的每一行除以另一个DataFrame向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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