Pythonic Way To Set Entire Column To Value Pandas (settingwithcopywarning)
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
Solution 1:
I was working on a similar requirement earlier today and I came across assign
. I got rid of the copy warning without using pd.options.mode.chained_assignment = None
. Here was my solution:
new_df_to_show_copy = dfp.loc[(dfp['A']>100) |(dfp['E']=='Unicorn')]
new_df_to_show_copy = new_df_to_show_copy.assign(Reason = 'what is with the copy warning')
# output:
A B C D E Reason
9999.00.0 USA Pharma NaN Unicorn what iswith the copy warning
assign
will leave a copy of the dataframe, and there is no inplace=True
parameter. so just reassigning the value worked for me.
Post a Comment for "Pythonic Way To Set Entire Column To Value Pandas (settingwithcopywarning)"