Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add together two columns in pandas without receiving SettingWithCopyWarning

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading