0

I need to overwrite certain values in a dataframe column, conditional on the values in another column.

The issue I have is that I can identify and replace certain rows with a string but do not know how to replace with data from another column

I have attempted the code below but have encountered the following error:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

when I run my example code shown below

df['Year'] = np.where(df['CallDtYear'] != 0, df['CallDtYear'], df['Year'])

I have also tried .iloc but don't know how to replace my chosen rows with data from another row, as opposed to a string value

My dataframe, df, is:

ID        CallDtYear  Year
EJ891119  2024        0
EJ522806  0           2023
ED766836  2019        0
EK089367  2023        2024
EK414703  2026        2026
EI684097  0           2021

And I want my expected output to yield

ID        CallDtYear  Year
EJ891119  2024        2024
EJ522806  0           2023
ED766836  2019        2019
EK089367  2023        2023
EK414703  2026        2026
EI684097  0           2021
Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45
malligator
  • 129
  • 2
  • 12

1 Answers1

2

You're close just use df.pop. df.pop will remove the column from the dataframe and returns its values:

df['Year'] = np.where(df['CallDtYear'] != 0, df['CallDtYear'], df.pop('Year'))
df

      ID    CallDtYear  Year
0   EJ891119    2024    2024
1   EJ522806    0       2023
2   ED766836    2019    2019
3   EK089367    2023    2023
4   EK414703    2026    2026
5   EI684097    0       2021
Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45
  • 1
    Thanks Mohit, this is great. As I understand it, you've effectively removed the original 'Year' column using pop and then recreated it with the desired data. This then gets around SettingWithCopyWarning as there are no conflicts as the original 'Year' column in df no longer exists. Is this how I should be thinking about it? – malligator Apr 03 '19 at 13:29
  • That's right :) – Mohit Motwani Apr 03 '19 at 13:30
  • 1
    Great, thanks again – malligator Apr 03 '19 at 13:33