python - 将 Pandas 数据框列导入为字符串而不是 int 或 float

标签 python pandas

我指的是这个问题。

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

建议的解决方案是这样的:

read_csv('sample.csv', dtype={'ID': object})

但是,如果我希望整个 csv 文件被读取为 str 列而不是 int 怎么办?每列都有一个字典会非常费力。有没有更清洁的方法?

最佳答案

您可以将参数 dtypestr 一起使用:

df = pd.read_csv('sample.csv', dtype=str)

关于python - 将 Pandas 数据框列导入为字符串而不是 int 或 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439271/

相关文章:

python - 使用 Python 的字符串的子字符串

python - Blender:如何从 python 脚本移动相机

python - 用 ggplot 绘制 2 geom_line()

python - 如何在seaborn中对轴进行排序,以便显示具有最多值的类别

python - 如何从 DataFrame TimeIndex 中提取频率

python - 子图中的分页符? Matplotlib 多页子图

python - 需要帮助理解一些带有 @、*args 和 **kwargs 的 Python 代码

python - 在 Django 中存储 5 分制的最有效方法

python - groupby后将子组合并到相邻子组中

Python Pandas Timeseries 如何找到值高于特定值的最大序列