Apply function to each row of pandas dataframe to create two new columns

后端 未结 4 1834
小蘑菇
小蘑菇 2020-12-02 14:36

I have a pandas DataFrame, st containing multiple columns:


DatetimeIndex: 53732 entries, 1993-01-0         


        
4条回答
  •  借酒劲吻你
    2020-12-02 14:46

    This was solved here: Apply pandas function to column to create multiple new columns?

    Applied to your question this should work:

    def calculate(s):
        a = s['path'] + 2*s['row'] # Simple calc for example
        b = s['path'] * 0.153
        return pd.Series({'col1': a, 'col2': b})
    
    df = df.merge(df.apply(calculate, axis=1), left_index=True, right_index=True)
    

提交回复
热议问题