繁体   English   中英

遍历列并重新分配值-Pandas / Python

[英]Iterating over columns and reassigning values - Pandas/Python

尝试使用for循环遍历列并将“是”和“否”更改为1和0。

出于某种原因,尝试执行此操作时收到无效的类型比较错误:

Panda DataFrame具有多个列,其中之一为“组合”

for col,row in d.iteritems():
    d.loc[d[col] == 'No', col] = 0
    d.loc[d[col] == 'Yes', col] = 1

TypeError:无效的类型比较

为了进行比较,我可以在没有问题的单个列上成功执行此操作:

d.loc[d['Combined'] == 'No', 'Combined'] = 0
d.loc[d['Combined'] == 'Yes', 'Combined'] = 1

为何将col的值插入loc函数以代替实际的列名会引发错误? 是否需要先将其转换为字符串或其他内容?

必须有采用整数值的列,对于这些行,这是“无效比较”。 因此,只需检查它是否为str的实例即可。

for col,row in d.iteritems():
    if isinstance(row[0], str):
        d.loc[d[col] == 'No', col] = 0
        d.loc[d[col] == 'Yes', col] = 1

并且出于同样的原因

d.loc[d['Combined'] == 'No', 'Combined'] = 0

这是完美的工作,因为它已经是带有字符串值的列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM