熊猫:如何正确识别标题?

时间:2020-09-05 07:59:30

标签: python pandas

我的文件如下:

enter image description here

与文本相同的文件

                      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

如何做到?

1 个答案:

答案 0 :(得分:1)

尝试一下

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
相关问题