根据条件修改 Pandas dataFrame 列值

时间:2021-02-14 11:41:13

标签: python pandas dataframe

我只想修改熊猫数据框列上大于 750 的值

datf.iloc[:,index][datf.iloc[:,index] > 750] = datf.iloc[:,index][datf.iloc[:,index] > 750]/10.7639

我认为语法很好,但我收到了 Pandas 警告,所以我不知道这样是否正确:

<ipython-input-24-72eef50951a4>:3: 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

在没有收到此警告的情况下执行此操作的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用 apply 方法使用自定义函数对列进行修改。 注意,您还可以将 applymap 用于多列

def my_func(x):
    if x > 750:
        x= #do your modification
    else:
       x
    return x

new_dta= datf['col_name'].apply(my_func)