0

The dataframe code works, however I'm getting this warning. I assume I need to use loc somewhere to prevent the slice copy and remove the warning, but not sure where.

df['splitter_size'] = df.groupby(['Cable in', 'Fibre in'])['Fibre in'].transform('count')
/usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:1637: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_block(indexer, value, name)
Blake
  • 1
  • 2
  • The hard thing to understand with this error is that it is informing you that you're _setting_ to a copy of a slice. Which means at some point _before_ this line you've made a copy of the DataFrame implicitly. And it is letting you know that you'll be modifying the copy, not the original df. You're not actually showing _where_ you've created a copy of the dataframe which is the source of this issue. – Henry Ecker Jul 30 '21 at 18:41
  • I've linked a duplicate which has a _ton_ of information. Just reminder, you're looking for something that looks like `new_df = df[something]` or `new_df = df.loc[something]` _before_ this groupby assignment. – Henry Ecker Jul 30 '21 at 18:43

0 Answers0