【发布时间】:2016-09-04 18:41:35
【问题描述】:
我有以下文件:
Contract, FG
9896342,Y
11037874,Y
6912529,Y
9896652,N
363291,Y
7348524,Y
6078482,Y
7795457,N
2486242,Y
3297980,Y
9760560,Y
1200533,N
11033963,N
7861603,Y
8218268,Y
9760247,Y
我想从此文件创建一个 pandas DF 并将列 Contract 用作字符串或 unicode 索引列。它看起来像数字,但从技术上讲,它是一个字符串。
我这样做了:DF = pd.read_csv('C:\\Users\\S.Benet\\Desktop\\test.txt', index_col='Contract', dtype=object, encoding = 'utf-8')
但索引被解释为 INT。
>>DF.index
Int64Index([ 9896342, 11037874, 6912529, 9896652, 363291, 7348524,
6078482, 7795457, 2486242, 3297980, 9760560, 1200533,
11033963, 7861603, 8218268, 9760247],
dtype='int64', name=u'Contract')
如何强制它成为字符串索引?
【问题讨论】:
标签: string python-2.7 pandas