0

I'm trying to duplicate a column in my DataFrame and could not get rid off the warning SettingWithCopyWarning :

I've tried :

df['new_col'] = df['old_col']
df.loc[:, 'new_col'] = df['old_col']
df.loc[:, 'new_col'] = df['old_col'].copy()
df.loc[:, 'new_col'] = df.loc[:,'old_col']

The result is always a Warning like this one

Warning (from warnings module):
  File "C:\Users\frank\Documents\orange\Python\Jira\jira_initiatives_dashboard.py", line 267
    df.loc[:, 'new_col'] = df['old_col']
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

Documentation does not seem to explain when there are multiple values instead of one value. What's wrong?

frank
  • 438
  • 1
  • 5
  • 19
  • 1
    What is your code before `df['new_col'] = df['old_col']` ? There is some filtering? Then use `copy` like `df[df.col > 1].copy()` – jezrael Dec 07 '22 at 08:49
  • Thanks it was before. I had a filter and needed to apply .copy() before adding calculated fields. – frank Dec 08 '22 at 19:37

0 Answers0