0

I want to change all values in a column of my pandas df to uppercase. The values are strings containing both letters and numbers.

I used the code below:

df['Cd'] = df['Cd'].str.upper()

The job was performed, however, I got the warning below:

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

I then tried the code below:

df['Cd'] = df['Cd'].apply(lambda x: str(x).upper())

I still ran to the same warning.

Please advice. Thanks!

smci
  • 32,567
  • 20
  • 113
  • 146
MAMS
  • 419
  • 1
  • 6
  • 17
  • What pandas version are you using? – Dani Mesejo Sep 18 '19 at 20:08
  • I don't thiink that should appear for older versions anyway right? Just for indexing on a column – yatu Sep 18 '19 at 20:09
  • 1
    This happens all the time, when you `df` is a slice of another (bigger) data. – Quang Hoang Sep 18 '19 at 20:10
  • I just tested this and it gives me no warnings for pandas 0.25 – Dani Mesejo Sep 18 '19 at 20:11
  • I don't recall getting it for just indexing on a column though, no slicing done here, but not sure on older versions – yatu Sep 18 '19 at 20:11
  • 1
    `df` is not the original data frame in the first place. You probably got it from another data frame, hence is a copy and whenever you modify it it'll raise that warning – rafaelc Sep 18 '19 at 20:13
  • The warning has nothing to do with uppercasing string values and everything to do with the fact that you're setting it on a dataframe which is itself a slice/copy of another dataframe. The title was misleading. – smci Sep 18 '19 at 20:15

0 Answers0