python - pandas 值正在尝试在 DataFrame 中的切片副本上设置
问题描述
这是我的代码:我的数据框 DF 基于此: index int64 FiscalYear int64 GL object GLID int64 GL_Debit float64 GL_Credit float64 month object Solde de fin float64
DF.sort_values(by=['GL', 'month'], inplace=True)
DF['Variation des périodes'] = DF.groupby([ 'GL'])['Solde de fin'].diff().fillna(DF['Solde de fin'])
DF = DF.rename(columns={'GL_Debit':'Débit année à date','GL_Credit':'Crédit année à date'})
for i in range(len(DF)):
row = DF.iloc[i]
teste = row['month'][5:7]
if row['month'][5:7]=='04':
if row['GL'][0] in ['4', '5', '6', '7', '8', '9']:
DF.iloc[i]['Variation des périodes'] = row['Solde de fin']
else:
DF.iloc[i]['Variation des périodes'] = row['Variation des périodes']
我有这个错误: SettingWithCopyWarning: A value is trying to be set on a slice of a copy from a DataFrame DF.iloc[i]['Variation des périodes'] = row['Solde de fin']
和
DF.iloc[i]['Variation des périodes'] 没有显示行 ['Solde de fin']
解决方案
我找到了一个解决方案:用 loc 替换 bloc 并且没有警告并且值是上帝!
DF.loc[i, 'Variation des périodes'] =row['Solde de fin']
推荐阅读
- amazon-web-services - AWS CognitoIdentity 的用户池 ID 的格式是什么?
- python - 如何在 Web 服务器上的 php 脚本上运行 python 文件?
- python - 从给定的日期范围获取一周的日期范围
- javascript - 将获取函数的结果分配给变量
- android - 来自房间的 LiveData 和 MutableLiveData 以显示错误消息
- c# - 如何设置用作字体选择器的 ComboBox 的 SelectedValue?
- c++ - 我们为什么使用
在 Turbo C++ 中? - azure-devops - 在 azure devops 中运行单元测试时,覆盖率返回 %NaN%
- amazon-s3 - aws S3的缓存控制元数据不在浏览器中缓存图像
- r - 按性别和值重新排序百分比堆积条形图