简体   繁体   English

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

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

Trying to use a for loop to iterate over columns and change Yes and No's to 1 and 0. 尝试使用for循环遍历列并将“是”和“否”更改为1和0。

For some reason, I am getting an invalid type comparison error when attempting this: 出于某种原因,尝试执行此操作时收到无效的类型比较错误:

Panda DataFrame has multiple columns, one of them being "Combined" Panda DataFrame具有多个列,其中之一为“组合”

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

TypeError: invalid type comparison TypeError:无效的类型比较

For comparison, I can successfully perform this on a single column without issues: 为了进行比较,我可以在没有问题的单个列上成功执行此操作:

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

Any reason why plugging the value of col into the loc function in place of the actual column name throws an error? 为何将col的值插入loc函数以代替实际的列名会引发错误? Does it need to be converted to a string or something before? 是否需要先将其转换为字符串或其他内容?

There must be columns which are taking integer values and for those rows its an "invalid comparison". 必须有采用整数值的列,对于这些行,这是“无效比较”。 So just check if its an instance of str and you are good to go. 因此,只需检查它是否为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

And for the same reason 并且出于同样的原因

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

this is working perfectly, as its already a column with string values. 这是完美的工作,因为它已经是带有字符串值的列。

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

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