1
import pandas as pd

df_run = pd.read_csv('UserEventSummary.csv')
df_run.accountId[0] = 'first-' + str(df_run.accountId[0])

The third line gives me this error:

/home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages/ipykernel/__main__.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':
/home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/indexing.py:194: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)

I've read the documentation on that and have a pretty good understanding of when this happens but not quite sure why I'm getting it here. I don't feel I'm doing anything wrong or dangerous!?

I'm guessing there is a better (more correct) way to do this?

DavidR
  • 6,622
  • 13
  • 56
  • 70

1 Answers1

1

Pandas has specific methods designed for reliably accessing and setting scalars. For scalar setting by label, use at. For scalar setting by integer positional index, use iat:

df_run['accountId'].iat[0] = f'first-{df_run["accountId"].iat[0]}'

As here, iat may be safely used for setting and accessing, thus avoiding the slice warning.

jpp
  • 159,742
  • 34
  • 281
  • 339