gpt4 book ai didi

python - pandas read_csv 将列转换为 int 类型

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:14 25 4
gpt4 key购买 nike

我下载了一个 csv,当我读取数据时,它打印出了 'Volume' 列:

0     623,446,600
1 371,965,300
2 260,752,300
3 116,561,400
Name: Volume, dtype: object

如何将其更改为 dtype: int

我尝试替换 , 但我不确定它是如何完成的。

最佳答案

我认为您需要在 read_csv 中使用参数 thousands=',' .

示例:

import pandas as pd
from pandas.compat import StringIO

temp=u"""Volume
0;623,446,600
1;371,965,300
2;260,752,300
3;116,561,400
"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", thousands=',')
print (df)
Volume
0 623446600
1 371965300
2 260752300
3 116561400

print (df.dtypes)
Volume int64
dtype: object

关于python - pandas read_csv 将列转换为 int 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42593666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com