0

A given column a of a data frame df contains strings with line-breaks. I split them into a list of strings via

df.a = df.a.str.split('\n', expand=False)

However, I get the warning

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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self[name] = value

However, I do not see any possibility to do it with .loc as suggested in the warning or here.

Am I right?

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
  • 1
    Strange I can't reproduce your error. Can you post a minimal example of your data? – jpp Mar 19 '18 at 10:57
  • 1
    This kind of error is very confused, because obviolusly use line of code bellow problematic line. So I guess problem is in line above `df.a = df.a.str.split('\n', expand=False)`. Maybe need [copy](https://stackoverflow.com/a/49316818/2901002) – jezrael Mar 19 '18 at 11:05
  • Thanks you, guys! This were exactly the hints I oversaw! – Michael Dorner Mar 19 '18 at 12:09

1 Answers1

0

Thanks to @jpp and @jezreal, the problem was in the previous code lines:

df = root_of_all_evil[some_selection]
df.a = df.a.str.split('\n', expand=False)

The solution is simple now as suggest by @jezreal:

df = root_of_all_evil[some_selection].copy()
df.a = df.a.str.split('\n', expand=False)
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117