【问题标题】:Pandas: how to recognize headings correctly?Pandas:如何正确识别标题?
【发布时间】:2020-09-05 07:59:30
【问题描述】:

我的文件如下所示:

与文本相同的文件:

                      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

我尝试用 pandas 阅读它:

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

怎么做?

【问题讨论】:

    标签: python pandas


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2019-07-15
      • 2011-01-17
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      相关资源
      最近更新 更多