Python 熊猫:如何正确识别标题?
我的文件如下所示: 与文本相同的文件:Python 熊猫:如何正确识别标题?,python,pandas,Python,Pandas,我的文件如下所示: 与文本相同的文件: Uranus Neptune Date, Time Lng Vel Lng Vel 01.01.2020 0:00:00 32.69520 -0.00884 346.26209 0.01919 01.01.2020 1:00:00 32.69483 -0
Uranus Neptune
Date, Time Lng Vel Lng Vel
01.01.2020 0:00:00 32.69520 -0.00884 346.26209 0.01919
01.01.2020 1:00:00 32.69483 -0.00881 346.26289 0.01921
01.01.2020 2:00:00 32.69446 -0.00877 346.26370 0.01923
01.01.2020 3:00:00 32.69410 -0.00874 346.26450 0.01926
01.01.2020 4:00:00 32.69373 -0.00870 346.26530 0.01928
01.01.2020 5:00:00 32.69337 -0.00867 346.26610 0.01930
01.01.2020 6:00:00 32.69301 -0.00863 346.26691 0.01932
我试着和熊猫一起读:
df_ephe = pd.read_csv(file, skiprows = 0, delim_whitespace=True)
但是Dataframe看起来不太好。列的标题不正确:
Uranus Neptune
Date, Time Lng Vel Lng Vel
01.01.2020 0:00:00 32.69520 -0.00884 346.26209 0.01919
1:00:00 32.69483 -0.00881 346.26289 0.01921
2:00:00 32.69446 -0.00877 346.26370 0.01923
3:00:00 32.69410 -0.00874 346.26450 0.01926
我需要像这样的东西:
Date, Time Uranus_Lng Uranus_Vel Neptune_Lng Neptune_Vel
01.01.2020 0:00:00 32.69520 -0.00884 346.26209 0.01919
1:00:00 32.69483 -0.00881 346.26289 0.01921
2:00:00 32.69446 -0.00877 346.26370 0.01923
3:00:00 32.69410 -0.00874 346.26450 0.01926
怎么做?试试这个
df = pd.read_csv(file, delim_whitespace=True)
pref = df.columns.to_list() * 2
df2 = df.reset_index()
df2 = df2.rename(columns=df2.iloc[0]).drop(df2.index[0])
df2.columns = list(df2.columns[:2]) + ['_'.join(x) for x in zip(pref[0::2] + pref[1::2], df2.columns[2:])]
print(df2)
输出:
Date Time Uranus_Lng Uranus_Vel Neptune_Lng Neptune_Vel
1 01.01.2020 0:00:00 32.69520 -0.00884 346.26209 0.01919
2 01.01.2020 1:00:00 32.69483 -0.00881 346.26289 0.01921
3 01.01.2020 2:00:00 32.69446 -0.00877 346.26370 0.01923
4 01.01.2020 3:00:00 32.69410 -0.00874 346.26450 0.01926
5 01.01.2020 4:00:00 32.69373 -0.00870 346.26530 0.01928
6 01.01.2020 5:00:00 32.69337 -0.00867 346.26610 0.01930
7 01.01.2020 6:00:00 32.69301 -0.00863 346.26691 0.01932