Issue
I need to conditionally update ColY below if the value for ColX != 0. The difference to other examples is that I need ColY to be replaced by the values from ColX, as opposed to a string
I can replace with a string using .loc when I use the following code:
df1.loc[df1.ColX != 0, 'ColY'] = 'Example'
How can I replace the relevant ColY values with the values from ColX? I have tried things such as the below to no avail
df1.loc[df1.ColX != 0, 'ColY'] = df1.ColX
My original dataframe, df1, is:
ID ColX ColY
A 2024 0
B 0 2023
C 2019 0
D 2023 2024
My desired output is:
ID ColX ColY
A 2024 2024
B 0 2023
C 2019 2019
D 2023 2023
Solution
Just for your convenience, here's another cleaner method in my opinion, using np.where
and .ne
:
df['ColY'] = np.where(df['ColX'].ne(0), df['ColX'], df['ColY'])
print(df)
ID ColX ColY
0 A 2024 2024
1 B 0 2023
2 C 2019 2019
3 D 2023 2023
Answered By - Erfan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.