How to add together two columns in pandas without receiving SettingWithCopyWarning
Question:
Trying to add add one column to another using pandas with:
merged_df.loc[:, 'airline_name'] = merged_df.loc[:, 'airline_name'] + merged_df['airline_icao_unique_code']
but every time I try I receive the warning :
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
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self.obj[selected_item_labels] = value
Process finished with exit code 0
Is there any way to do this without triggering this warning? I have tried altering the syntax and creating temporary variables but I cant seem to get it to work. I have also tried using the .copy()
function but still no dice.
Answers:
merged_df['airline_name'] = merged_df['airline_name'] + merged_df['airline_icao_unique_code']
This is likely due to merged_df
being a subset of another DataFrame
. The easiest way to fix this is to use .copy()
before performing this operation:
merged_df = merged_df.copy()
merged_df['airline_name'] = merged_df['airline_name'] + merged_df['airline_icao_unique_code']
Trying to add add one column to another using pandas with:
merged_df.loc[:, 'airline_name'] = merged_df.loc[:, 'airline_name'] + merged_df['airline_icao_unique_code']
but every time I try I receive the warning :
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
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self.obj[selected_item_labels] = value
Process finished with exit code 0
Is there any way to do this without triggering this warning? I have tried altering the syntax and creating temporary variables but I cant seem to get it to work. I have also tried using the .copy()
function but still no dice.
merged_df['airline_name'] = merged_df['airline_name'] + merged_df['airline_icao_unique_code']
This is likely due to merged_df
being a subset of another DataFrame
. The easiest way to fix this is to use .copy()
before performing this operation:
merged_df = merged_df.copy()
merged_df['airline_name'] = merged_df['airline_name'] + merged_df['airline_icao_unique_code']