当我尝试从数据帧中删除列“header=”时:
#import data
for path in filepaths:
df = pd.read_csv(r"{}".format(path), header=6, sep="delimiter", engine="python")
#delete column "Header="
del df['Header=']
出现以下错误:
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Header='
当我尝试drop函数时:
for path in filepaths:
df = pd.read_csv(r"{}".format(path), header=6, sep="delimiter", engine="python")
df.drop("Header=", axis=1, inplace=True)
出现以下错误:
raise KeyError(f"{labels[mask]} not found in axis")
KeyError: "['Header='] not found in axis"
从这些错误看来,我似乎没有一个名为“header=”的列,但是当我运行这段代码时:
for path in filepaths:
df = pd.read_csv(r"{}".format(path), header=6, sep="delimiter", engine="python")
all_files.append(df)
print(df.columns)
我得到以下输出:
Index(['Header=,"PK","RT","Area Pct","Library/ID","Ref","CAS","Qual"'], dtype='object')
其中“header=”是列名之一
最后,当我打印时,数据的头看起来是这样的:
print(df.head)
<bound method NDFrame.head of Header=,"PK","RT","Area Pct","Library/ID","Ref","CAS","Qual"
0 1=, 1, 1.3671,27.3306,"Nitrogen oxide (N2O) ...
1 2=, 2, 2.5023, 0.2947,"Methyl-d3 1-Dideuteri...
2 3=, 3, 4.1156, 0.6791,"Heptane, 2,4-dimethyl...
3 4=, 4, 4.6653, 0.3038,"Octane, 4-methyl- (CA...
4 5=, 5, 4.7131,32.2856,"Acetamide, N,N-dimeth...
5 6=, 6, 6.1590,28.7519,"Phenol (CAS) $$ Izal ...
6 7=, 7, 6.3144, 0.1824,"Acetamide, 2-chloro- ...
7 8=, 8, 6.8044, 5.7176,"l-Limonene $$ Cyclohe...
8 9=, 9, 7.0911, 1.1867,"DECANE, 3,7-DIMETHYL-...
9 10=, 10, 7.1509, 0.3323,"(R,R)-3,8-Dimethylde...
10 11=, 11, 7.5811, 0.6304,"DECANE, 3,7-DIMETHYL...
11 12=, 12, 7.6408, 0.1889,"Decane, 4-methyl- (C...
12 13=, 13, 8.0949, 0.2466,"6-Aza-5,7,12,14-tetr...
13 14=, 14, 9.4094, 0.5843,"Hexadecane, 2,6,10,1...
14 15=, 15, 9.8038, 0.3497,"2,4-di(trimethylsilo...
15 16=, 16, 9.8396, 0.1937,"Eicosane (CAS) $$ n-...
16 17=, 17, 11.2736, 0.5516,"TETRADECAMETHYLCYCLO...
17 18=, 18, 11.4290, 0.1900,"Phenol, 2,4-bis(1,1-... >
尝试在读取CSV时使用选项sep=“,”
更改分隔符。 问题是你正在读取整行作为标题。