Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将Int64Index转换为Index(从CSV读取)?_Python_Pandas - Fatal编程技术网

Python 如何将Int64Index转换为Index(从CSV读取)?

Python 如何将Int64Index转换为Index(从CSV读取)?,python,pandas,Python,Pandas,我有一个CSV文件,内容包括: 然后我通过以下方式读取此文件: A = pd.read_csv("MyTest2.csv") A.columns 输出为 Index(['ID', '202005'], dtype='object') 但是,如果我传输数据帧并删除一些未使用的列,方法是: A = pd.read_csv("MyTest2.csv") A = A.T A = A.rename(columns=A.iloc[0]) A = A.drop(A

我有一个CSV文件,内容包括:

然后我通过以下方式读取此文件:

A = pd.read_csv("MyTest2.csv")
A.columns
输出为

Index(['ID', '202005'], dtype='object')
但是,如果我传输数据帧并删除一些未使用的列,方法是:

A = pd.read_csv("MyTest2.csv")
A = A.T
A = A.rename(columns=A.iloc[0])
A = A.drop(A.index[0])
A.columns
输出将变为:

Int64Index([1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243], dtype='int64')
我的问题是我想使用[“1234”]来读取列值,而不是[1234](不带双引号)

如何将Int64Index转换为Index?或者是一种正确的方法来防止索引在传输计算期间变为Int64Index(或RangeIndex?

  • ID
    列转换为
    str
    类型,使用
    .astype
A=pd.read_csv(“MyTest2.csv”)#创建数据帧
A.ID=A.ID.astype('str')#将ID转换为str类型
  • 读取文件时设置
    dtype
A=pd.read\u csv('MyTest2.csv',dtype={'ID':str})