我通过此代码获取了错误的列名: 为什么会这样?有没有其他方法可以做到这一点?
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
答案 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