python - 在循环中转换 Pandas Dataframes 值

标签 python pandas

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

相关文章:

Python - 数据框中每个字符串的 str.match

python-3.x - 使用 CSV 查询网站时出现问题,输入不正确

python - 按值排序的多重索引 - Pandas

python - web2py 网络服务器 - 保持与外部 SQL 服务器连接的最佳方式?

python - 通过超时取消异步迭代器

python - 使用 Python 将电子邮件发送到带有内联图像的 Gmail

python - Cython:如何包装返回 C++ 对象的 C++ 函数?

python - 将一列空列表添加到 DataFrame

python - pandas:如何将嵌套 JSON 解包为数据帧?

python - 根据 pandas 数据框中的相邻列将 NaN 值替换为特定文本