Reputation: 366
I have the following dataframe:
df = pd.DataFrame({'ISIN': ['A1kT23', '4523', 'B333', '49O33'], 'Name': ['Example A', 'Name Xy', 'Example B', 'Test123'], 'Sector_x': ['test1', 'test2', 'test3', 'test4'], 'Sector_y': ['abc', '', '', 'xyz']})
I would like to replace value in column Sector_y by using column Sector_x, if Sector_y = ''
so that I get the following result:
df = pd.DataFrame({'ISIN': ['A1kT23', '4523', 'B333', '49O33'], 'Name': ['Example A', 'Name Xy', 'Example B', 'Test123'], 'Sector_x': ['test1', 'test2', 'test3', 'test4'], 'Sector_y': ['abc', 'test2', 'test3', 'xyz']})
I tried using the code
df['Sector_y'] = np.where('',['Sector_x'],['Sector_y'])
but didn't deliver the result I wanted.
Any suggestions how to solve the problem?
Upvotes: 0
Views: 6055
Reputation: 7509
Another option is to use apply
row-wise, and simply compare string across columns using the or
operator (taking advantage from the fact that empty strings evaluate to False
):
df['Sector_y'] = df.apply(lambda row: row['Sector_y'] or row['Sector_x'], axis=1)
output:
ISIN Name Sector_x Sector_y
0 A1kT23 Example A test1 abc
1 4523 Name Xy test2 test2
2 B333 Example B test3 test3
3 49O33 Test123 test4 xyz
Upvotes: -1
Reputation: 323226
Fix np.where
df['Sector_y'] = np.where(df['Sector_y'] =='', df['Sector_x'], df['Sector_y'])
Upvotes: 1
Reputation: 23217
You can use .loc
to specify the filtering condition and specify the target column Sector_y
and assign with values from column Sector_x
, as follows:
df.loc[df['Sector_y'] =='', 'Sector_y'] = df['Sector_x']
Result:
print(df)
ISIN Name Sector_x Sector_y
0 A1kT23 Example A test1 abc
1 4523 Name Xy test2 test2
2 B333 Example B test3 test3
3 49O33 Test123 test4 xyz
Upvotes: 3