如何在按列遍历数据帧时在 for 循环中使用 dtype?这样我就可以遍历特定 dtype 列的每一行
How to use dtype inside a for loop while traversing a dataframe column-wise ? So that I can traverse each row of a particular dtype column
例如,我有一个这样的数据框df
:
| Name | color | id | weight |
|------- |-------- |---- |-------- |
| john | blue | 67 | 70 |
| clara | yellow | - | 67 |
| diana | red | 89 | 56 |
这里像“id”和“weight”这样的数字列应该有所有数值,不像“id”的第二个值是“-”。
如果我df.dtypes
,它returns:
| name | object
| color | object
| id | object
| weight | float
**我如何按列遍历数据框,然后检查列的类型是否为对象,如果是对象,则检查它是否由于错字“-”而成为对象喜欢 id- 如果是,则举旗 **
将列名和数据类型压缩成一个元组:
for col_name, col_type in zip(df.columns, df.dtypes):
if col_type == "object":
# do whatever here
pass
例如,我有一个这样的数据框df
:
| Name | color | id | weight |
|------- |-------- |---- |-------- |
| john | blue | 67 | 70 |
| clara | yellow | - | 67 |
| diana | red | 89 | 56 |
这里像“id”和“weight”这样的数字列应该有所有数值,不像“id”的第二个值是“-”。
如果我df.dtypes
,它returns:
| name | object
| color | object
| id | object
| weight | float
**我如何按列遍历数据框,然后检查列的类型是否为对象,如果是对象,则检查它是否由于错字“-”而成为对象喜欢 id- 如果是,则举旗 **
将列名和数据类型压缩成一个元组:
for col_name, col_type in zip(df.columns, df.dtypes):
if col_type == "object":
# do whatever here
pass