alwayscurious
alwayscurious

Reputation: 1165

combining columns in pandas dataframe

I have the following dataframe:

df = pd.DataFrame({
         'user_a':['A','B','C',np.nan],
         'user_b':['A','B',np.nan,'D']
})

current df

I would like to create a new column called user and have the resulting dataframe:

complete df

What's the best way to do this for many users?

Upvotes: 1

Views: 52

Answers (2)

Ysh Xiong
Ysh Xiong

Reputation: 77

use .apply method:

In [24]: df = pd.DataFrame({'user_a':['A','B','C',np.nan],'user_b':['A','B',np.nan,'D']})

In [25]: df
Out[25]: 
  user_a user_b
0      A      A
1      B      B
2      C    NaN
3    NaN      D

In [26]: df['user'] = df.apply(lambda x: [i for i in x if not pd.isna(i)][0], axis=1)

In [27]: df
Out[27]: 
  user_a user_b user
0      A      A    A
1      B      B    B
2      C    NaN    C
3    NaN      D    D

Upvotes: 0

jezrael
jezrael

Reputation: 862661

Use forward filling missing values and then select last column by iloc:

df = pd.DataFrame({
         'user_a':['A','B','C',np.nan,np.nan],
         'user_b':['A','B',np.nan,'D',np.nan]
})

df['user'] = df.ffill(axis=1).iloc[:, -1]
print (df)
  user_a user_b user
0      A      A    A
1      B      B    B
2      C    NaN    C
3    NaN      D    D
4    NaN    NaN  NaN

Upvotes: 3

Related Questions