想要使用 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/