大 pandas 读取以逗号分隔的千位分隔符格式的 CSV 数据 [英] pandas reading CSV data formatted with comma for thousands separator

查看:38
本文介绍了大 pandas 读取以逗号分隔的千位分隔符格式的 CSV 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以分号分隔的 CSV 在 Pandas 中创建一个数据框,并使用逗号作为数字数据的千位分隔符.有没有办法读取它,以便列的类型是浮点数而不是字符串?

I am trying to create a dataframe in pandas using a CSV that is semicolon-delimited, and uses commas for the thousands separator on numeric data. Is there a way to read this in so that the type of the column is float and not string?

推荐答案

将参数 thousands=',' 传递给 read_csv 将这些值读取为数千:

Pass param thousands=',' to read_csv to read those values as thousands:

In [27]:
import pandas as pd
import io

t="""id;value
0;123,123
1;221,323,330
2;32,001"""
pd.read_csv(io.StringIO(t), thousands=r',', sep=';')

Out[27]:
   id      value
0   0     123123
1   1  221323330
2   2      32001

这篇关于大 pandas 读取以逗号分隔的千位分隔符格式的 CSV 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆