pandas combine two columns with null values

后端 未结 6 1952
醉梦人生
醉梦人生 2021-02-03 22:10

I have a df with two columns and I want to combine both columns ignoring the NaN values. The catch is that sometimes both columns have NaN values in which case I want the new co

6条回答
  •  忘了有多久
    2021-02-03 22:46

    1. You can replace the non zero values with column names like

      df1= df.replace(1, pd.Series(df.columns, df.columns))

    2. Replace 0's with empty string and then merge the columns like below

      f = f.replace(0, '') f['new'] = f.First+f.Second+f.Three+f.Four

    Refer the full code below.

    import pandas as pd
    df = pd.DataFrame({'Second':[0,1,0,0],'First':[1,0,0,0],'Three':[0,0,1,0],'Four':[0,0,0,1], 'cl': ['3D', 'Wireless','Accounting','cisco']})
    df2=pd.DataFrame({'pi':['Accounting','cisco','3D','Wireless']})
    df1= df.replace(1, pd.Series(df.columns, df.columns))
    f = pd.merge(df1,df2,how='right',left_on=['cl'],right_on=['pi'])
    f = f.replace(0, '')
    f['new'] = f.First+f.Second+f.Three+f.Four
    

    df1:

    In [3]: df1                                                                                                                                                                              
    Out[3]: 
       Second  First  Three  Four          cl
    0       0  First      0     0          3D
    1  Second      0      0     0    Wireless
    2       0      0  Three     0  Accounting
    3       0      0      0  Four       cisco
    

    df2:

    In [4]: df2                                                                                                                                                                              
    Out[4]: 
               pi
    0  Accounting
    1       cisco
    2          3D
    3    Wireless
    

    Final df will be:

    In [2]: f                                                                                                                                                                                
    Out[2]: 
       Second  First  Three  Four          cl          pi     new
    0          First                       3D          3D   First
    1  Second                        Wireless    Wireless  Second
    2                 Three        Accounting  Accounting   Three
    3                        Four       cisco       cisco    Four
    

提交回复
热议问题