Python: Divide each row of a DataFrame by another DataFrame vector

前端 未结 5 2091
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 20:26

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 dime

5条回答
  •  佛祖请我去吃肉
    2020-12-08 20:59

    You can divide by the series i.e. the first row of df2:

    In [11]: df = pd.DataFrame([[1., 2.], [3., 4.]], columns=['A', 'B'])
    
    In [12]: df2 = pd.DataFrame([[5., 10.]], columns=['A', 'B'])
    
    In [13]: df.div(df2)
    Out[13]: 
         A    B
    0  0.2  0.2
    1  NaN  NaN
    
    In [14]: df.div(df2.iloc[0])
    Out[14]: 
         A    B
    0  0.2  0.2
    1  0.6  0.4
    

提交回复
热议问题