Import pandas dataframe column as string not int(将 pandas 数据框列导入为字符串而不是 int)
问题描述
我想将以下 csv 作为字符串而不是 int64 导入.Pandas read_csv 自动将其转换为 int64,但我需要此列作为字符串.
I would like to import the following csv as strings not as int64. Pandas read_csv automatically converts it to int64, but I need this column as string.
ID
00013007854817840016671868
00013007854817840016749251
00013007854817840016754630
00013007854817840016781876
00013007854817840017028824
00013007854817840017963235
00013007854817840018860166
df = read_csv('sample.csv')
df.ID
>>
0 -9223372036854775808
1 -9223372036854775808
2 -9223372036854775808
3 -9223372036854775808
4 -9223372036854775808
5 -9223372036854775808
6 -9223372036854775808
Name: ID
不幸的是,使用转换器会得到相同的结果.
Unfortunately using converters gives the same result.
df = read_csv('sample.csv', converters={'ID': str})
df.ID
>>
0 -9223372036854775808
1 -9223372036854775808
2 -9223372036854775808
3 -9223372036854775808
4 -9223372036854775808
5 -9223372036854775808
6 -9223372036854775808
Name: ID
推荐答案
只是想重申这将适用于 pandas >= 0.9.1:
Just want to reiterate this will work in pandas >= 0.9.1:
In [2]: read_csv('sample.csv', dtype={'ID': object})
Out[2]:
ID
0 00013007854817840016671868
1 00013007854817840016749251
2 00013007854817840016754630
3 00013007854817840016781876
4 00013007854817840017028824
5 00013007854817840017963235
6 00013007854817840018860166
我也在创建一个关于检测整数溢出的问题.
I'm creating an issue about detecting integer overflows also.
在此处查看解决方案:https://github.com/pydata/pandas/issues/2247
更新,因为它可以帮助他人:
Update as it helps others:
要将所有列作为str,可以这样做(来自评论):
To have all columns as str, one can do this (from the comment):
pd.read_csv('sample.csv', dtype = str)
要将大多数或选择性列作为str,可以这样做:
To have most or selective columns as str, one can do this:
# lst of column names which needs to be string
lst_str_cols = ['prefix', 'serial']
# use dictionary comprehension to make dict of dtypes
dict_dtypes = {x : 'str' for x in lst_str_cols}
# use dict on dtypes
pd.read_csv('sample.csv', dtype=dict_dtypes)
这篇关于将 pandas 数据框列导入为字符串而不是 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 pandas 数据框列导入为字符串而不是 int
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01