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

前端 未结 5 2083
佛祖请我去吃肉
佛祖请我去吃肉 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:43

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

    or you can use df1/df2.values[0,:]

提交回复
热议问题