PANDAS: 从 csv 文件导入数据时列名错误
PANDAS: Wrong column names while importing data from csv file
我通过这段代码得到了错误的列名:
为什么会这样?还有其他方法吗?
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
这是因为 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
我通过这段代码得到了错误的列名: 为什么会这样?还有其他方法吗?
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
这是因为 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