runawaykid
runawaykid

Reputation: 1441

How do I combine two columns within a dataframe in Pandas?

Say I have two columns, A and B, in my dataframe:

A  B
1  NaN
2  5
3  NaN
4  6

I want to get a new column, C, which fills in NaN cells in column B using values from column A:

A  B   C
1  NaN 1
2  5   5
3  NaN 3
4  6   6

How do I do this?

I'm sure this is a very basic question, but as I am new to Pandas, any help will be appreciated!

Upvotes: 5

Views: 857

Answers (3)

user517696
user517696

Reputation: 2672

df['c'] = df['b'].fillna(df['a'])

So what .fillna will do is it will fill all the Nan values in the data frame We can pass any value to it Here we pass the value df['a'] So this method will put the corresponding values of 'a' into the Nan values of 'b' And the final answer will be in 'c'

Upvotes: 2

eumiro
eumiro

Reputation: 213125

You can use combine_first:

df['c'] = df['b'].combine_first(df['a'])

Docs: http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.combine_first.html

Upvotes: 6

Colonel Beauvel
Colonel Beauvel

Reputation: 31181

You can use where which is a vectorized if/else:

df['C'] = df['A'].where(df['B'].isnull(), df['B'])

   A   B  C
0  1 NaN  1
1  2   5  5
2  3 NaN  3
3  4   6  6

Upvotes: 5

Related Questions