我收到一条设置为copy的警告消息
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py:670: 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 iloc._setitem_with_indexer(indexer, value)
几个小时来,我一直在努力想办法解决这个问题,但无法找出问题所在。有人能透露一点信息吗
代码如下:
df = pd.read_excel(path).assign(rate = np.NaN, alert = np.NaN)
def incidence(data, treshold):
#iterates through each row of dataset
for i, row in enumerate(df.itertuples()):
try:
#divides total_rdtpost by total_rdtused
data.rate.iloc[i] = row[5]/row[7]
except ZeroDivisionError:
#fixes the ZeroDivisionError, please read https://docs.python.org/3/library/exceptions.html#ZeroDivisionError
data.rate.iloc[i] = 0.0
#creates a low or high record depending on the treshold variable
if data.rate.iloc[i] > treshold:
data.alert.iloc[i] = "high"
else:
data.alert.iloc[i] = "low"
return data
incidence(df, 0.2)
另外,我正在使用Colab
data.rate
和data.alert
分别是data['rate']
和data['alert']
的缩写data['rate']
可以复制,这样做{将这些更改为:
您可以提前保存列索引:
顺便说一句,for循环引用传递给函数的全局/外部
df
,而不是data
。应该是:另外,您还可以将函数中的所有步骤作为列操作来执行,而不是使用
itertuples
。您正在修改函数中的数据帧相关问题 更多 >
编程相关推荐