在read_csv文件之后,我有一个DataFrame,其中标头位于标头和第0行中,如下所示:
Unnamed: 0 Unnamed: 1 Temp Hi Low Out Dew Wind Wind.1 Wind.2 Hi.1 ... In In In .1 In .2 In .3 In Air Unnamed: 30 Wind.4 Wind.5 ISS Arc.
0 Date Time Out Temp Temp Hum Pt. Speed Dir Run Speed ... Temp Hum Dew Heat EMC Density ET Samp Tx Recept Int.
1 09/04/19 11:05 --- --- --- --- --- 0.0 --- 0.00 0.0 ... 27.8 30 8.6 26.6 6.05 1.1483 0.00 0 1 0.0 5
2 09/04/19 11:10 --- --- --- --- --- 0.0 --- 0.00 0.0 ... 28.1 29 8.4 26.8 5.85 1.1475 0.00 0 1 0.0 5
3 09/04/19 11:15 --- --- --- --- --- 0.0 --- 0.00 0.0 ... 28.2 29 8.5 27.0 5.85 1.1468 0.00 0 1 0.0 5
我想将实际的标头与第0行合并,并在它们之间设置一个空格(例如,数据从第1行到最后)。怎么做? 问候。
答案 0 :(得分:2)
当您读取csv文件时,您可以将header
指向前两行,然后在columns
上会有多个索引
df = pd.read_csv('test.csv', header=[0, 1])
df.columns=df.columns.map(' '.join)
答案 1 :(得分:0)
我将读取不带标题的文件,转置和连接:
df = pd.read_csv('filename.csv', header=None).T
df['new index'] = df[0].fillna('').str + ' ' + df[1].str
df.set_index('new index', inplace=True)
df.drop([0,1], inplace=True, axis=1)
df = df.T