【问题标题】:Modifying entries of a pandas dataframe within a loop在循环中修改熊猫数据框的条目
【发布时间】:2017-01-28 16:02:55
【问题描述】:

我想为我使用 for 循环的数据框中添加每条记录的概率

def map_score(dataframe,customers,prob):
  dataframe['Propensity'] = 0
  for i in range(len(dataframe)):
      for j in range(len(customers)):
          if dataframe['Client'].iloc[i] == customers[j]:
              dataframe["Propensity"].iloc[i] = prob[j]

我能够正确映射与每个客户端关联的概率,但 Python 会抛出警告消息

试图在 DataFrame 的切片副本上设置一个值。 尝试改用 .loc[row_indexer,col_indexer] = value

请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 从 ipykernel 导入 kernelapp 作为应用程序

当我使用 .loc 函数时,结果是错误的,我得到的是空值。 请提出一个有条件地更新和添加条目的好方法

【问题讨论】:

    标签: python loops pandas dataframe


    【解决方案1】:

    您正试图在副本上进行分配。
    dataframe["Propensity"] 是一列,但是dataframe 的“副本”。

    但是,您正在使用i 跟踪索引位置。那么当你有一个列名"Propensity" 和一个索引位置i 时,你如何使用.loc

    分配一些变量,比如idx,在那个位置等于dataframe.index

    idx = dataframe.index[i]
    

    然后您可以使用.loc 进行分配并且没有问题

    dataframe.loc[idx, "Propensity"] = prob[j]
    

    def map_score(dataframe,customers,prob):
      dataframe['Propensity'] = 0
      for i in range(len(dataframe)):
          idx = dataframe.index[i]
          for j in range(len(customers)):
              if dataframe['Client'].iloc[i] == customers[j]:
                  dataframe.loc[idx, "Propensity"] = prob[j]
    

    【讨论】:

    • 非常感谢,我现在明白了。在这种情况下,有没有办法避免循环内循环。我基本上想将 dataframe.clients 与具有客户端的数组匹配,并且概率数组与客户端数组具有相同的顺序。所以基本上我想将概率添加到客户端匹配的索引中
    • 这个问题是关于警告的。我会问另一个关于循环的问题。当你这样做时,请添加一些示例数据,以便我们看到你所看到的。
    • 感谢 piRSquared,感谢您的宝贵反馈:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多