Make new column in Panda dataframe by adding values from other columns

前端 未结 10 993
情歌与酒
情歌与酒 2020-12-13 01:59

I have a dataframe with values like

A B
1 4
2 6
3 9

I need to add a new column by adding values from column A and B, like

         


        
相关标签:
10条回答
  • 2020-12-13 02:10

    You could use sum function to achieve that as @EdChum mentioned in the comment:

    df['C'] =  df[['A', 'B']].sum(axis=1)
    
    In [245]: df
    Out[245]: 
       A  B   C
    0  1  4   5
    1  2  6   8
    2  3  9  12
    
    0 讨论(0)
  • 2020-12-13 02:10

    I wanted to add a comment responding to the error message n00b was getting but I don't have enough reputation. So my comment is an answer in case it helps anyone...

    n00b said:

    I get the following warning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

    He got this error because whatever manipulations he did to his dataframe prior to creating df['C'] created a view into the dataframe rather than a copy of it. The error didn't arise form the simple calculation df['C'] = df['A'] + df['B'] suggested by DeepSpace.

    Have a look at the Returning a view versus a copy docs.

    0 讨论(0)
  • 2020-12-13 02:13

    Concerning n00b's comment: "I get the following warning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead"

    I was getting the same error. In my case it was because I was trying to perform the column addition on a dataframe that was created like this:

    df_b = df[['colA', 'colB', 'colC']]
    

    instead of:

    df_c = pd.DataFrame(df, columns=['colA', 'colB', 'colC'])
    

    df_b is a copy of a slice from df
    df_c is an new dataframe. So

    df_c['colD'] = df['colA'] + df['colB']+ df['colC']
    

    will add the columns and won't raise any warning. Same if .sum(axis=1) is used.

    0 讨论(0)
  • 2020-12-13 02:29

    The simplest way would be to use DeepSpace answer. However, if you really want to use an anonymous function you can use apply:

    df['C'] = df.apply(lambda row: row['A'] + row['B'], axis=1)
    
    0 讨论(0)
提交回复
热议问题