Reputation: 2079
Imagine that I have a single Dataframe as such:
df = pd.DataFrame([[1,2,3,None],[1,2,3,None],[1,2,3,None],[None,2,3,1]], columns=["A","B","C","AA"])
A | B | C | AA |
---|---|---|---|
1 | 2 | 3 | |
1 | 2 | 3 | |
2 | 3 | 1 |
Column AA is actually the same as A, but has suffered a typo somewhere in the data processing pipeline precious steps.
How can I actually rename ['AA'] to ['A'] and move the non-missing values? Example:
A | B | C |
---|---|---|
1 | 2 | 3 |
1 | 2 | 3 |
1 | 2 | 3 |
I imagine that if I do:
df['A'] = df['AA']
Null values will be copied.
So, any hints here?
Upvotes: 0
Views: 153
Reputation: 28709
You could try combine_first:
In [8]: df.assign(A=df.A.combine_first(df.AA)).drop(columns='AA')
Out[8]:
A B C
0 1.0 2 3
1 1.0 2 3
2 1.0 2 3
3 1.0 2 3
Upvotes: 1
Reputation: 403
To add to @mullinscr, first sum the columns and then drop the 'AA' column
df['A'] = df[['A','AA']].sum(axis=1)
df.drop('AA', axis=1, inplace=True)
Upvotes: 0
Reputation: 1748
Sum them both together:
df['A'] = df[['A','AA']].sum(axis=1)
Result is:
A B C AA
0 1.0 2 3 NaN
1 1.0 2 3 NaN
2 1.0 2 3 NaN
3 1.0 2 3 1.0
Upvotes: 0