【问题标题】:Transform pandas dataframe from tsv to parquet将 pandas 数据帧从 tsv 转换为 parquet
【发布时间】:2020-12-12 00:45:38
【问题描述】:

我正在尝试对 pandas 数据框进行旋转,但我猜是“扭曲”?这是加载到 pandas 中的输入表(tsv)。

timestamp   sensor      type    unit    value
1607724078  sensor_a    string  gps     coords1
1607724078  sensor_b    int     bar     1
1607724079  sensor_a    string  gps     coords5
1607724079  sensor_b    int     bar     4
1607724080  sensor_a    string  gps     coords9
1607724080  sensor_b    int     bar     7

输出应该是这样的

timestamp   sensor_a sensor_a_type sensor_a_unit sensor_b sensor_b_type sensor_b_unit
1607724078  coords1  string        gps           1        int           bar
1607724079  coords5  string        gps           4        int           bar
1607724080  coords9  string        gps           7        int           bar

如您所见,它应该按时间戳分组,并且传感器列的值必须定义为具有原始值列的值的单独列。此外,原始类型和单位列必须与新的 sensor_x 列“名称合并”并保留它们的值。

目标是将其保存为镶木地板。重要提示:可以有两个以上的传感器。

现在我完成了创建一个包含我需要的所有信息的 MultiIndexed 数据框,但我无法将它写入镶木地板,因为我猜 MultiIndex 必须以某种方式展平,以便传感器单元和类型有自己的独立列。

import pandas as pd

if __name__ == '__main__':
    df = pd.read_csv("data/test.tsv", delimiter='\t', index_col='timestamp')
    df2 = df.pivot(columns=['sensor', 'unit', 'type'], values='value')
    print(df2)

输出

sensor     sensor_a sensor_b sensor_c
unit            gps      bar  percent
type         string      int   double
timestamp                            
1607724078  coords1        1    11.11
1607724079  coords5        4    44.44
1607724080  coords9        7    77.77

提前致谢!

【问题讨论】:

    标签: python pandas csv parquet


    【解决方案1】:

    这是一个部分答案...只是要重命名一些列:

    from io import StringIO
    sList = '''timestamp   sensor      type    unit    value
    1607724078  sensor_a    string  gps     coords1
    1607724078  sensor_b    int     bar     1
    1607724079  sensor_a    string  gps     coords5
    1607724079  sensor_b    int     bar     4
    1607724080  sensor_a    string  gps     coords9
    1607724080  sensor_b    int     bar     7'''.split('\n')
    s = '\n'.join([','.join(l.split()) for l in sList])
    
    with StringIO(s) as sio:
        df = pd.read_csv(sio)
    
    df[df.sensor == 'sensor_a'].merge(df[df.sensor == 'sensor_b'], on='timestamp', suffixes=['_a','_b'])
    

    输出

        timestamp   sensor_a    type_a  unit_a  value_a sensor_b    type_b  unit_b  value_b
    0   1607724078  sensor_a    string  gps coords1 sensor_b    int bar 1
    1   1607724079  sensor_a    string  gps coords5 sensor_b    int bar 4
    2   1607724080  sensor_a    string  gps coords9 sensor_b    int bar 7
    

    【讨论】:

      【解决方案2】:

      您可以根据this solution 仅将列替换为顶层

      df2.columns = df2.columns.get_level_values(0)
      

      这应该会导致类似

                  sensor_a sensor_b sensor_c
      1607724078  coords1        1    11.11
      1607724079  coords5        4    44.44
      1607724080  coords9        7    77.77
      

      【讨论】:

        【解决方案3】:

        您可以使用 set_index 和 unstack 重塑数据框,然后使用列表推导展平多索引,如下所示:

        import pandas as pd    
        from io import StringIO
        
        #Input file
        sList = '''timestamp   sensor      type    unit    value
        1607724078  sensor_a    string  gps     coords1
        1607724078  sensor_b    int     bar     1
        1607724079  sensor_a    string  gps     coords5
        1607724079  sensor_b    int     bar     4
        1607724080  sensor_a    string  gps     coords9
        1607724080  sensor_b    int     bar     7'''
        
        #Read file in as dataframe
        df = pd.read_csv(StringIO(sList), sep='\s\s+', engine='python')
        
        #Reshape the dataframe
        df_out = df.set_index(['timestamp', 'sensor']).unstack()
        
        #Flatten multiindex
        df_out.columns = [f'{j}_{i}' if i != 'value' else f'{j}' for i, j in  df_out.columns]
        
        df_out = df_out.reset_index()
        print(df_out)
        

        输出:

            timestamp sensor_a_type sensor_b_type sensor_a_unit sensor_b_unit sensor_a sensor_b
        0  1607724078        string           int           gps           bar  coords1        1
        1  1607724079        string           int           gps           bar  coords5        4
        2  1607724080        string           int           gps           bar  coords9        7
        

        【讨论】:

          猜你喜欢
          • 2017-02-04
          • 1970-01-01
          • 1970-01-01
          • 2021-03-20
          • 1970-01-01
          • 2021-11-16
          • 2016-09-27
          • 2021-09-15
          相关资源
          最近更新 更多