0

this piece of code throws the well-known and much-discussed "A value is trying to be set on a copy of a slice from a DataFrame" error. I have read several of the threads about the topic on this site but couldn't find any useful information.

df = load_data() # comes straight from pandas.read_sql()

f_200 = df.Programm.str.contains(r'(?:-CMF|8$)')
df['Groesse'] = '150'
df['Groesse'][f_200] = '200' # this throws the warning

For the purpose of this question I tried to reproduce the error with this minimal piece of code:

a = pandas.DataFrame([1,2], columns=["a"])
i = pandas.Series([True, False])
a['a'][i] = 10

However, this works fine. I don't understand this. Is there anything the matter with a data frame obtained from a call to pandas.read_sql()?

musbur
  • 567
  • 4
  • 16

1 Answers1

1

I had an inkling that .loc would help, so I tried all of these:

df.loc['Groesse'][f_200]
df['Groesse'].loc[f_200]
df.loc['Groesse', f_200]

None of them worked. I had seen the post jezrael referred to, but the first time I hadn't paid attention that the row indexer comes first. So I tried this:

df.loc[f_200, 'Groesse']

and it works. Thanks!

musbur
  • 567
  • 4
  • 16