【问题标题】:How to make operations between columns in pandas so to avoid warning message 'Try using .loc[row_indexer,col_indexer] = value instead' [duplicate]如何在 pandas 中的列之间进行操作以避免警告消息“尝试使用 .loc[row_indexer,col_indexer] = value 而不是”[重复]
【发布时间】:2022-02-23 10:18:35
【问题描述】:

我有一个如下所示的数据框:

Time  y1    y2
100   130  44.57
200   130  42.23
300   130  42.83

我只是想将列时间从 s 转换为分钟,并在列 y1 和 y2 之间进行操作。但是我不断收到警告消息: 试图在 DataFrame 中的切片副本上设置一个值。 尝试改用 .loc[row_indexer,col_indexer] = value

#Both this operations give a warning error: 
df1['Time'] =  df1['Time']/60)
df1['y1/y2'] = (df1['y2']-df1['y1'])/(df1['y2'])

#This is what I tried:
#df1.loc[:, 'New'] = df1.loc[:,'Time'].apply(lambda x: df1['Time']/60)

#df1['y1/y2'] = (df1['y2']-df1['y1'])/(df1['y2']).copy()

#What I'm trying to do is very simple but I keep getting the error message: 

#A value is trying to be set on a copy of a slice from a DataFrame.
#Try using .loc[row_indexer,col_indexer] = value instead

提前谢谢你!

【问题讨论】:

  • df1 是如何构建的?它可能是更大 DataFrame 的一部分。
  • 你的前两行对我来说很好。接下来的两行注释掉了不必要的复杂。
  • 我也没有收到任何警告

标签: pandas indexing


【解决方案1】:

在进行计算之前添加此行:

df1 = df1.copy()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 2022-11-29
    • 2018-12-22
    • 1970-01-01
    • 2021-11-30
    • 2017-11-28
    • 2023-03-11
    相关资源
    最近更新 更多