【问题标题】:reshape dataframe time series重塑数据帧时间序列
【发布时间】:2021-02-26 01:43:33
【问题描述】:

[![在此处输入图像描述][1]][1]我有一个特定形状的天气数据的数据框,我想对其进行转换,但正在苦苦挣扎。 我的数据框看起来像这样:

    city temp_day1, temp_day2, temp_day3 ...., hum_day1, hum_day2, hum_day4, ..., condition
    

    city_1      12         13             20         44      44.5    good          44  
    city_1      12         13             20         44      44.5   

不好 44 city_2 14 04 33 44 44.5
好 44

我想把它改成

    city_1                                   city_2                          .....
day. temperature humidity condition ...   temperature humidity condition

1      12           44      good .          12         13             
20         44      44.5 
2      13           44 .5   bad  . 
3      20           NaN     bad  .
4      NaN          44       .

有一天没有温度值和湿度值

感谢您的帮助

【问题讨论】:

    标签: python-3.x pandas dataframe time-series


    【解决方案1】:

    使用wide_to_longDataFrame.unstack,最后使用DataFrame.swaplevelDataFrame.sort_index

    df1 = (pd.wide_to_long(df, 
                           stubnames=['temp','hum'], 
                           i='city', 
                           j='day', 
                           sep='_', 
                           suffix='\w+')
            .unstack(0)
            .swaplevel(1,0, axis=1)
            .sort_index(axis=1))
    print (df1)
    city city_1      
            hum  temp
    day              
    day1   44.0  12.0
    day2   44.5  13.0
    day3    NaN  20.0
    day4   44.0   NaN
    

    替代解决方案:

    df1 = df.set_index('city')
    df1.columns = df1.columns.str.split('_', expand=True)
    df1 = df1.stack([0,1]).unstack([0,1])
    

    如果需要从index提取号码:

    df1 = (pd.wide_to_long(df, 
                           stubnames=['temp','hum'], 
                           i='city', 
                           j='day', 
                           sep='_', 
                           suffix='\w+')
            .unstack(0)
            .swaplevel(1,0, axis=1)
            .sort_index(axis=1))
    
    df1.index = df1.index.str.extract('(\d+)', expand=False)
    print (df1)
    city city_1      
            hum  temp
    day              
    1      44.0  12.0
    2      44.5  13.0
    3       NaN  20.0
    4      44.0   NaN
    

    编辑:

    真实数据解决方案:

    df1 = df.set_index(['condition', 'ACTIVE', 'mode', 'apply', 'spy', 'month'], append=True) 
    df1.columns = df1.columns.str.split('_', expand=True) 
    df1 = df1.stack([0,1]).unstack([0,-2])
    

    如果需要删除MultiIndex中不必要的级别:

    df1 = df1.reset_index(level=['condition', 'ACTIVE', 'mode', 'apply', 'spy', 'month'], drop=True)
    

    【讨论】:

    • 评论不用于扩展讨论;这个对话是moved to chat
    • 我放了一张数据的屏幕截图
    • 我放了一个样本,因为有300多列
    • 我刚刚删除了图片,对不起
    • 如何共享数据文件?
    【解决方案2】:

    您可以像这样使用 pandas 转置方法:df.T

    这会将您的数据框变成一行。如果您创建多个列,则可以通过索引对其进行切片并将每个切片分配给独立的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多