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.
- How to deal with SettingWithCopyWarning in Pandas
- Pandas DataFrame: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
- SettingWithCopyWarning even when using .loc[row_indexer,col_indexer] = value
- Python Pandas Warning: A value is trying to be set on a copy of a slice from a DataFrame
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