I have a pandas dataframe with a pair of columns where, on every row, one cell is a nan and the other is not. It looks like this:
Var1 | Var2 | |
---|---|---|
0 | 3 | nan |
1 | 8 | nan |
2 | nan | 6 |
3 | 4 | nan |
4 | nan | 2 |
5 | nan | 6 |
I would like to merge these two columns in one without the nans:
Var1 | |
---|---|
0 | 3 |
1 | 8 |
2 | 6 |
3 | 4 |
4 | 2 |
5 | 6 |
Any ideas?
Advertisement
Answer
Use fillna
, setting values
parameter to another Series.
JavaScript
x
11
11
1
df['Var1'].fillna(df['Var2'], inplace=True)
2
df['Var1']
3
4
# Var1
5
# 0 3
6
# 1 8
7
# 2 6
8
# 3 4
9
# 4 2
10
# 5 6
11