I have the following dataframe with two columns 'Department' and 'Project'.
data = [['Sales', '', 1000], ['R&D', 'Alpha', 2000], ['Purchase', 'DE', 1500],['Communication', '', 2000], ['HR', '', 10020]]
df = pd.DataFrame(data, columns = ['Department', 'Project', 'Amount'])
print(df)
Department Project Amount
0 Sales 1000
1 R&D Alpha 2000
2 Purchase DE 1500
3 Communication 2000
4 HR 10020
I want to replace the Department entry by the Project entry if the Project entry is not empty. So the dataframe looks like that:
Department Project Amount
0 Sales 1000
1 Alpha Alpha 2000
2 DE DE 1500
3 Communication 2000
4 HR 10020
How can I do this?
Try with pandas.DataFrame.where
:
df["Department"] = df["Project"].where(df["Project"].ne(''), df["Department"])
>>> df
Department Project Amount
0 Sales 1000
1 Alpha Alpha 2000
2 DE DE 1500
3 Communication 2000
4 HR 10020
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With