PANDAS:从csv文件导入数据时列名错误

时间:2018-01-05 05:40:14

标签: python pandas csv import-from-csv

我通过此代码获取了错误的列名: 为什么会这样?有没有其他方法可以做到这一点?

names = {'SYMBOL','CLMP'}
data2 = pd.read_csv(filePath +"\\"+ fileNameCM2+'.csv',skiprows = 1, index_col=False, names = names,header=None)
df3 = pd.DataFrame(data2)

print(df3.head())

OUTPUT:
          CLMP    SYMBOL
0         ACC    853921
1    ADANIENT   2758466
2  ADANIPORTS   7591819
3  ADANIPOWER  11774513
4  AJANTPHARM    257752

EXPECTED:
      SYMBOL      CLMP
0         ACC    853921
1    ADANIENT   2758466
2  ADANIPORTS   7591819
3  ADANIPOWER  11774513
4  AJANTPHARM    257752

1 个答案:

答案 0 :(得分:1)

因为set会对名称进行排序,而是使用list列名称:

names = ['SYMBOL','CLMP']
df3 = pd.read_csv(filePath +"\\"+ fileNameCM2+'.csv',
                  skiprows = 1, 
                  index_col=False, 
                  names = names)

print (df)
       SYMBOL      CLMP
0         ACC    853921
1    ADANIENT   2758466
2  ADANIPORTS   7591819
3  ADANIPOWER  11774513
4  AJANTPHARM    257752