使用iloc后,仍在使用COPYWARNING进行设置。它来自哪里?

2024-06-28 19:37:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到一条设置为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


Tags: ofthepandasdfdataratevalueon
1条回答
网友
1楼 · 发布于 2024-06-28 19:37:24

data.ratedata.alert分别是data['rate']data['alert']的缩写data['rate']可以复制,这样做{}仍然是一个副本

将这些更改为:

data.iloc[i, data.columns.get_loc('rate')] = ...

您可以提前保存列索引:

def incidence(data, treshold):
    # at the start of the function
    rate_col = data.columns.get_loc('rate')
    alert_col = data.columns.get_loc('alert')
    ...
    for ...
        # later
        data.iloc[i, rate_col] = ...

顺便说一句,for循环引用传递给函数的全局/外部df,而不是data。应该是:

for i, row in enumerate(data.itertuples()):

另外,您还可以将函数中的所有步骤作为列操作来执行,而不是使用itertuples。您正在修改函数中的数据帧

相关问题 更多 >