切片警告的 Python 数据帧副本


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

第三行给了我这个错误:

/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)

我已经阅读了有关此的文档,并且对何时发生这种情况有很好的了解,但不太确定为什么我在这里得到它。我不觉得我做错了什么或危险!?

我猜有更好(更正确)的方法可以做到这一点吗?

Pandas 具有用于可靠地访问和设置标量的特定方法。对于按标签设置标量,请使用 at 。对于按整数位置索引设置标量,请使用iat

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

如此处,iat可以安全地用于设置访问,从而避免切片警告。

最新更新