如何从csv创建pandas数据帧,并将数值保持为字符串,而不将其更改为int?

2024-04-24 11:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

Python

有没有办法从csv文件创建熊猫数据帧,而不将数值转换为int

示例:

在csv文件中,将值另存为“0123456”。 当我尝试读取该表时,值转换为int,然后删除第一个零值'0',我看到它是123456

谢谢


Tags: 文件csv数据示例int数值办法将值
1条回答
网友
1楼 · 发布于 2024-04-24 11:28:43

如果您查看文档中的^{},您可以使用

dtype : Type name or dict of column -> type, optional Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.

因此,您可以使用pd.read_csv(..., dtype='str', ...),或者更精确地按列指定它

相关问题 更多 >