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

Trying to create a new column delivers A value is trying to be set on a copy of a slice from a DataFrame

I have a problem. I want to create a new column adress. Before that I want to get only all columns where namecode === code but unfortunately I got an error A value is trying to be set on a copy .... I looked at (see below) but nothing worked for me.

    customerId  code    namecode    name    street          adresscode
0   1           1       1           Mike    Long Street     458
1   2           1       1           Jucie   Short Street    856
2   3           9999    48           Max    Average Street  874

import pandas as pd

import pandas as pd
d = {'customerId': [1, 2, 3],
     'code': [1, 1, 9999],
     'name_code': [1, 1, 48],
     'name': ['Mike', 'Jucie', 'Max'],
     'street': ['Long Street', 'Short Street', 'Average Street'],
     'adresscode': ['458', '856', '874']
    }
df_old = pd.DataFrame(data=d)

display(df_old)

df_new = df_old.loc[df_old['code'] == df_old['name_code']]


>>> df_new['adress'] = df_new ['name'].copy() + df_new ['street'].copy() + df_new ['adresscode'].copy()

[OUT]
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


>>> df_new['adress'] = df_new .loc['name','street','adresscode']

[OUT]
IndexingError: Too many indexers


What I want

    customerId  code    namecode    name    street          adresscode adress
0   1           1       1           Mike    Long Street     458        Mike Long Street 458
1   2           1       1           Jucie   Short Street    856        Jucie Short Street 856

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

>Solution :

You need to copy when slicing, not when assigning:

df_new = df_old.loc[df_old['code'] == df_old['name_code']].copy()
df_new['adress'] = df_new['name'] + df_new['street'] + df_new['adresscode']

output (without SettingWithCopyWarning):

   customerId  code  name_code   name        street adresscode  \
0           1     1          1   Mike   Long Street        458   
1           2     1          1  Jucie  Short Street        856   

                 adress  
0    MikeLong Street458  
1  JucieShort Street856

For you other slicing, you need to use a list of columns:

df_new = df_old[['name','street','adresscode']].copy()
# OR
df_new = df_old.loc[:, ['name','street','adresscode']].copy()
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