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

后端 未结 4 1826
小蘑菇
小蘑菇 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)
    
    0 讨论(0)
  • 2020-12-02 14:49

    Yet another solution based on Assigning New Columns in Method Chains:

    st.assign(a = st['path'] + 2*st['row'], b = st['path'] * 0.153)
    

    Be aware assign always returns a copy of the data, leaving the original DataFrame untouched.

    0 讨论(0)
  • 2020-12-02 14:53

    To make the first approach work, try returning a Series instead of a tuple (apply is throwing an exception because it doesn't know how to glue the rows back together as the number of columns doesn't match the original frame).

    def calculate(s):
        a = s['path'] + 2*s['row'] # Simple calc for example
        b = s['path'] * 0.153
        return pd.Series(dict(col1=a, col2=b))
    

    The second approach should work if you replace:

    st.ix[i]['a'] = a
    

    with:

    st.ix[i, 'a'] = a
    
    0 讨论(0)
  • 2020-12-02 15:07

    I always use lambdas and the built-in map() function to create new rows by combining other rows:

    st['a'] = map(lambda path, row: path + 2 * row, st['path'], st['row'])
    

    It might be slightly more complicated than necessary for doing linear combinations of numerical columns. On the other hand, I feel it's good to adopt as a convention as it can be used with more complicated combinations of rows (e.g. working with strings) or filling missing data in a column using functions of the other columns.

    For example, lets say you have a table with columns gender, and title, and some of the titles are missing. You can fill them with a function as follows:

    title_dict = {'male': 'mr.', 'female': 'ms.'}
    table['title'] = map(lambda title,
        gender: title if title != None else title_dict[gender],
        table['title'], table['gender'])
    
    0 讨论(0)
提交回复
热议问题