pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

后端 未结 5 552
广开言路
广开言路 2020-11-22 06:24

I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian,

5条回答
  •  名媛妹妹
    2020-11-22 06:44

    Since this is the first Google result for 'pandas new column from others', here's a simple example:

    import pandas as pd
    
    # make a simple dataframe
    df = pd.DataFrame({'a':[1,2], 'b':[3,4]})
    df
    #    a  b
    # 0  1  3
    # 1  2  4
    
    # create an unattached column with an index
    df.apply(lambda row: row.a + row.b, axis=1)
    # 0    4
    # 1    6
    
    # do same but attach it to the dataframe
    df['c'] = df.apply(lambda row: row.a + row.b, axis=1)
    df
    #    a  b  c
    # 0  1  3  4
    # 1  2  4  6
    

    If you get the SettingWithCopyWarning you can do it this way also:

    fn = lambda row: row.a + row.b # define a function for the new column
    col = df.apply(fn, axis=1) # get column data with an index
    df = df.assign(c=col.values) # assign values to column 'c'
    

    Source: https://stackoverflow.com/a/12555510/243392

    And if your column name includes spaces you can use syntax like this:

    df = df.assign(**{'some column name': col.values})
    

    And here's the documentation for apply, and assign.

提交回复
热议问题