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

标签 python pandas

我下载了一个 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/

相关文章:

python - 在 Python 中使用 MultiIndex 和 to_excel 时创建的标题下方的空行

python - seaborn 散点图日期时间 x 轴太宽

python - 使用基于示例的代码暂停动画时遇到问题

python - 基于 Pandas 数据框替换 numpy 二维数组中的值

python - 在Python中解析时间戳

python - 如何让 Python 从日期中提取工作日?

python - 将可迭代字典展开/扩展到数据框中

python - 将数据集中的列类型转换为python中特定格式的日期时间类型时出错

python - python pandas 如果在过去 N 天内满足条件则创建一个新列

python - 如何在 Python 3.1 中安装 argparse?