I want to set an entire column to a single string value. When doing so I get the (ever so popular) SettingWithCopy
. I've tried to search SO before posting about this specific issue.
import pandas as pd
import numpy as np
dfp = pd.DataFrame({'A' : [1,21,8,44,np.NaN,6,75,8,44,999],
'B' : [1,1,3,5,0,0,np.NaN,9,np.NaN,0],
'C' : ['AA1233445','AA1233445', 'rmacy','Idaho Rx','Ab123455','TV192837','RX','Ohio Drugs','RX12345','USA Pharma'],
'D' : [123456,123456,1234567,12345678,12345,12345,12345678,123456789,1234567,np.NaN],
'E' : ['Assign','Assign','Hello','Ugly','Appreciate','Undo','Testing','Unicycle','Pharma','Unicorn',]})
print(dfp)
new_df_to_show_copy = dfp.loc[(dfp['A']>100) |(dfp['E']=='Unicorn')]
new_df_to_show_copy['Reason'] = 'what is with the copy warning'
Now I can get rid of the warning with
new_df_to_show_copy = dfp.loc[(dfp['A']>100) |(dfp['E']=='Unicorn')].copy() <--Notice copy()
new_df_to_show_copy['Reason'] = 'what is with the copy warning'
And I know I can get rid of the warning with pd.options.mode.chained_assignment = None
but I feel like that's "cheating". I'm looking at the documentation, but can't find a minimal way for setting an entire column to a single value without adding .copy()
or suppressing the warning. What's the best way to do that?