Reputation: 1033
I have columns A and B, where column A rarely has data, and column B is completely full. I want to copy column B to column A and always preserve any value that column A has over column B.
The length of my dataframe is around 1.5 million, so df.iterrows() is a solution, however it would be very time consuming. Is there any optimized pandas function or trick to do this as efficiently as possible?
Upvotes: 1
Views: 777
Reputation: 2286
I would use .loc to get this done
df.loc[df['A'].isnull(), 'A'] = df['B']
This reads like so: located where column 'A' is null and set column 'A' equal to column 'B'.
Upvotes: 2
Reputation: 8132
You can try something like
df["A"][df["A"].isna()] = df["B"][df["A"].isna()]
Edit: the commenter pointed out that there's already a method in pandas for exactly this problem, so use that instead.
Upvotes: 1