gpt4 book ai didi

python - 在循环中转换 Pandas Dataframes 值

转载 作者:行者123 更新时间:2023-12-01 06:39:21 25 4
gpt4 key购买 nike

想要使用 object 转换数据帧值 n , y ,和?0 , 1 ,和0

这是df.head() :

df.head()
party infants water budget physician salvador religious satellite aid missile immigration synfuels education superfund crime duty_free_exports eaa_rsa
0 republican n y n y y y n n n y ? y y y n y
1 republican n y n y y y n n n n n y y y n ?
2 democrat ? y y ? y y n n n n y n y y n n
3 democrat n y y n ? y n n n n y n y n n y
4 democrat y y y n y y n n n n y ? y y y y

我尝试使用简单的 for循环:

for names in df.columns.values:
df.names.replace(('n', 'y'), (0, 1), inplace=True)
df.names.replace('?', 0, inplace=True)

但它返回给我一个AttributeError: 'DataFrame' object has no attribute 'names'

请与我分享任何转换 object 的想法值进入int值。

最佳答案

我认为你可以使用DataFrame.replace没有就地:

df = df.replace(('n','?','y'), (0,0,1))
#alternative
df = df.replace({'n':0,'?':0,'y':1})
<小时/>
print (df)
party infants water budget physician salvador religious \
0 republican 0 1 0 1 1 1
1 republican 0 1 0 1 1 1
2 democrat 0 1 1 0 1 1
3 democrat 0 1 1 0 0 1
4 democrat 1 1 1 0 1 1

satellite aid missile immigration synfuels education superfund \
0 0 0 0 1 0 1 1
1 0 0 0 0 0 1 1
2 0 0 0 0 1 0 1
3 0 0 0 0 1 0 1
4 0 0 0 0 1 0 1

crime duty_free_exports eaa_rsa
0 1 0 1
1 1 0 0
2 1 0 0
3 0 0 1
4 1 1 1

通常不建议inplace - link :

The pandas core team discourages the use of the inplace parameter, and eventually it will be deprecated (which means "scheduled for removal from the library"). Here's why:

inplace won't work within a method chain.
The use of inplace often doesn't prevent copies from being created, contrary to what the name implies.
Removing the inplace option would reduce the complexity of the pandas codebase.

在您的代码中,names 是列名称,您只想替换此列的值:

df.names.replace

错误意味着没有列names:

AttributeError: 'DataFrame' object has no attribute 'names'

关于python - 在循环中转换 Pandas Dataframes 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526981/

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