gpt4 book ai didi

python-3.x - 替换数据框中的 0's with 1'

转载 作者:行者123 更新时间:2023-12-01 02:30:43 25 4
gpt4 key购买 nike

我有一个数据框 df,它有 A,B,C,D,...,X,Y,Z 列,我希望替换所有 code>0 个条目,在 CD 列之外的所有条目中都有 1

我尝试的以下代码是:

df[df.columns[~df.columns.isin(['C','D'])]].replace(0,1,inplace=True)

但它不起作用。

编码新手,如有任何帮助,我们将不胜感激!

最佳答案

I have a data frame df which has columns A,B,C,D,...,X,Y,Z and I wish to replace all 0 entries with 1 in all but columns C and D.

# Save the columns
c = df['C']
d = df['D']

# Remove the columns
df.drop('C', axis=1, inplace=True)
df.drop('D', axis1=, inplace=True)

# Make your replacement
df.replace(0, 1)

# Replace the columns
df['C'] = c
df['D'] = d

关于python-3.x - 替换数据框中的 0's with 1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61228270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com