尝试理解并修复带有CopyWarning的设置

时间:2018-08-14 03:22:24

标签: python pandas

我正在运行一个程序,在其中调用API,将输出转换为数据框,然后尝试调整数据框中的某些项目。我用于执行此操作的代码完成了我想要的,但是它引发了SettingWithCopyWarning。我尝试阅读此书,但不完全了解在给定情况下如何避免这种情况。

这是我的代码:

df = pd.DataFrame(data)

#edit names to match
entry = df[['DriverID', 'Number', 'Name']]
entry['Name'].loc[entry['Name'] == 'Darrell Wallace Jr'] = 'Bubba Wallace'
entry['Driver'] = entry['Name'].str.lower()
entry = entry[['DriverID', 'Number', 'Driver']]

entry.to_csv('api_entry_list_' + race_id + '.csv', index=False)

这是警告输出:

C:\Python36\lib\site-packages\pandas\core\indexing.py:189: 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)
C:\EclipseWorkspaces\csse120\Personal\NASCAR_Projects\Other\api_entry_list.py:40: 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
  entry['Name'].loc[entry['Name'] == 'Darrell Wallace Jr'] = 'Bubba Wallace'
C:\EclipseWorkspaces\csse120\Personal\NASCAR_Projects\Other\api_entry_list.py:41: SettingWithCopyWarning: 
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
  entry['Driver'] = entry['Name'].str.lower()

0 个答案:

没有答案