Reputation: 129
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
Upvotes: 1
Views: 4386
Reputation: 42916
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
Upvotes: 2
Reputation: 1070
The problem with df1.loc[df1.ColX != 0, 'ColY'] = df1.ColX
is that you're trying to replace a subset of df1.ColY
(i.e. where df1.ColX != 0
) with the entire df1.ColX
, which has more values.
To copy the correct values conditionally, you also have to apply the same filter to df1.ColX
:
df1 = pd.DataFrame(data=[[2024, 0], [0, 2023], [2019, 0], [2023, 2023],], columns=['ColX', 'ColY'])
relevant_cols = (df1.ColX != 0)
df1.loc[relevant_cols, 'ColY'] = df1.loc[relevant_cols, 'ColX']
df1
# ColX ColY
# 0 2024 2024
# 1 0 2023
# 2 2019 2019
# 3 2023 2023
Upvotes: 1