切片警告的Python数据框副本

时间:2019-01-03 22:25:26

标签: python python-3.x pandas dataframe

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)

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

我猜有更好的(更正确的)方法吗?

1 个答案:

答案 0 :(得分:1)

Pandas具有为可靠访问和设置标量而设计的特定方法。要通过标签进行标量设置,请使用at。要通过整数位置索引进行标量设置,请使用iat

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

就像这里一样,iat可以安全地用于设置访问,从而避免出现切片警告。