尝试将一列中的数据从字符串转换为整数

时间:2019-05-16 01:34:12

标签: python pandas dataframe

我有一个名为df['code']的数据框,看起来像这样。

0          1
1          2
2          3
3          6
4          9
5         12
6         15

其中第一列是索引,第二列是我想要转换为整数的列。

我想做的是:
for i in range(len(df['code'])): df['code'][i] = int(df['code'][i])

这不起作用。有任何想法吗?谢谢

1 个答案:

答案 0 :(得分:1)

我认为做到这一点的一种方法是:

for i in range(len(df['code'])):
    df['code'].iloc[i] = int(df['code'].iloc[i] )

它不使用索引本身,而是使用索引位置。

相关问题