pandas: add new column with value from either of two other columns
pd.DataFrame({'A':[None,2,None,None,3,4],'B':[1,2,3,4,5,6]})
A B
0 NaN 1
1 2 2
2 NaN 3
3 NaN 4
4 3 5
5 4 6
how do I add column C
that will take the value from column A
if it's not NaN, otherwise column B
's value?
A B C
0 NaN 1 1
1 2 2 2
2 NaN 3 3
3 NaN 4 4
4 3 5 3
5 4 6 4
Answer
You can use the fillna()
method in pandas to achieve this. Specifically, you want to fill the NaN
values in column A
with the corresponding values from column B
. Here’s how you can do it:
Code:
import pandas as pd
# Creating the DataFrame
df = pd.DataFrame({'A': [None, 2, None, None, 3, 4], 'B': [1, 2, 3, 4, 5, 6]})
# Creating column C by filling NaN values in A with values from B
df['C'] = df['A'].fillna(df['B'])
print(df)
Output:
A B C
0 NaN 1 1
1 2 2 2
2 NaN 3 3
3 NaN 4 4
4 3 5 3
5 4 6 4
Explanation:
- The
fillna()
method fillsNaN
values in the given column (A
in this case) with the corresponding values from another column (B
). - The new column
C
will contain the values fromA
where they are notNaN
, and fromB
whereA
hasNaN
.
This method is efficient and handles the replacement without needing loops or conditionals.