提问人:shubham tiwari 提问时间:7/29/2023 更新时间:7/29/2023 访问量:34
谁能解释一下 Pandas 中的 SettingWithCopyWarning 是什么 [duplicate]
Can anyone explain what is SettingWithCopyWarning in Pandas [duplicate]
问:
我是熊猫的新手,在为我的课程做作业时,我遇到了一个警告,上面写着
e:\Python.py\coursera_data\1_week3_assign.py:10: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
我想知道这到底是什么意思?我必须更改我的代码吗?
法典
import pandas as pd
import numpy as np
import re
csv=pd.read_excel('Energy Indicators.xls',skiprows=17,usecols=[2,3,4,5],skipfooter=1)
csv=csv.rename(columns={'Unnamed: 2':'Country','Petajoules':'Energy Supply','Gigajoules':'Energy Supply per Capita','%':'% Renewable'})
csv['Energy Supply'].replace(['...'],[np.nan],inplace=True)
for i in range(227):
csv['Country'][i]=re.sub(pattern='\(.*?\)',repl='',string=csv['Country'][i])
csv['Country'][i]=re.sub(pattern='\d',repl='',string=csv['Country'][i])
csv['Energy Supply']=csv['Energy Supply']*1000000
print(csv.head())
完整的警告消息
e:\Python.py\coursera_data\1_week3_assign.py:8: 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
csv['Country'][i]=re.sub(pattern='\(.*?\)',repl='',string=csv['Country'][i])
e:\Python.py\coursera_data\1_week3_assign.py:9: 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
csv['Country'][i]=re.sub(pattern='\d',repl='',string=csv['Country'][i])
答:
0赞
Navkar Jain
7/29/2023
#1
当您尝试修改作为原始数据视图的 DataFrame 的子集时,通常会出现警告“SettingWithCopyWarning”,并且 pandas 警告您更改可能不会像您预期的那样反映在原始 DataFrame 中。
在您的代码中,之所以引发警告,是因为您正在使用索引 csv['Country'][i] 修改“Country”列。
可以使用 .loc 访问器修改原始 DataFrame 中的“Country”列。这是你如何做到的:
csv.loc[i, 'Country'] = re.sub(pattern='\(.*?\)', repl='', string=csv.loc[i, 'Country'])
csv.loc[i, 'Country'] = re.sub(pattern='\d', repl='', string=csv.loc[i, 'Country'])
评论