我想将以下csv作为字符串而不是int64导入。熊猫read_csv自动将其转换为int64,但我需要将此列作为字符串。ID00013007854817840016671868000130078548178400167492510001300785481784001675463000013007854817840016781876000130078548178400170288240001300785481784001796323500013007854817840018860166df = read_csv('sample.csv')df.ID>>0 -92233720368547758081 -92233720368547758082 -92233720368547758083 -92233720368547758084 -92233720368547758085 -92233720368547758086 -9223372036854775808Name: ID不幸的是,使用转换器会得到相同的结果。df = read_csv('sample.csv', converters={'ID': str})df.ID>>0 -92233720368547758081 -92233720368547758082 -92233720368547758083 -92233720368547758084 -92233720368547758085 -92233720368547758086 -9223372036854775808Name: ID
2 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
这可能不是最优雅的方法,但是可以完成工作。
In[1]: import numpy as np
In[2]: import pandas as pd
In[3]: df = pd.DataFrame(np.genfromtxt('/Users/spencerlyon2/Desktop/test.csv', dtype=str)[1:], columns=['ID'])
In[4]: df
Out[4]:
ID
0 00013007854817840016671868
1 00013007854817840016749251
2 00013007854817840016754630
3 00013007854817840016781876
4 00013007854817840017028824
5 00013007854817840017963235
6 00013007854817840018860166
只需替换'/Users/spencerlyon2/Desktop/test.csv'为文件的路径
添加回答
举报
0/150
提交
取消