简体   繁体   中英

How to combine two columns with the same name

I have a Pandas DataFrame that has two columns named: "cases". I would like to merge both columns into one, I don't want to perform any calculation.

If cases1 contains "NaN", then cases2 contains data and viceversa.

cases1 and cases2 cannot both contain data for the same row.

             cases    cases    deaths
2020-01-01   10       NaN      0   
2020-01-02   NaN      2        1

The output should be:

             cases    deaths
2020-01-01   10       0   
2020-01-02   2        1

You could stat the dataframe over with just df[['deaths']] and use .assign() to create a new column cases that adds the two columns together by column position with .iloc :

df = df[['deaths']].assign(cases=df.iloc[:,0:2].fillna(0).sum(axis=1))

         deaths cases
2020-01-01    0  10.0
2020-01-02    1  2.0
length_row = df.shape[0]
cases = np.empty(length_row)
for row in df.iterrows(): 
    if row[0].isnull() == True:
        cases.append(row[1])
    elif row[1].isnull() == True: 
        cases.append(row[0]) 
df.drop(['cases'] , axis = 1) 
df['cases'] = cases 
   

don't know if there is a mistake and also its a bit long, not wise as previous answer though

  • get the length of columns -create an empty np array -iterate over rows by index on 'cases' columns -drop the 'cases' columns from dataframe -add new array to dataframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM