大 pandas :多层数据帧中的数据透视表 [英] pandas: pivot table inside multilevel dataframe

查看:216
本文介绍了大 pandas :多层数据帧中的数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试透视表格,以便在列中转换一些行值,所以从这个数据框中 df_behave

I'm trying to pivot table, in order to transform some rows values in columns, so from this dataframe df_behave

list 
                   date_time      field      value
 1    0 2015-05-22 05:37:59      StudentID   129
      1 2015-05-22 05:37:59      SchoolId    3
      2 2015-05-22 05:37:59      GroupeId     45
 2    3 2015-05-26 05:56:59      StudentID   129
      4 2015-05-26 05:56:59      SchoolId     65
      5 2015-05-26 05:56:59      GroupeId    13
      6 2015-05-26 05:56:59      Reference     87
 3    ......................    ......  ......

为了实现:

list 
                      date_time     StudentID   SchoolId  GroupId    Reference
     1       2015-05-22 05:37:59      129           3         45

     2      2015-05-26 05:56:59      129            65        15       87   

     3    ......................    ......  ......

包含以下代码:

def calculate():
    df_behave['value'] = df_behave['value'].astype(int)
    pi_df=pd.pivot_table(df_behave, 'value', index=['date_time'], columns='field')
    return pi_df

我尝试过一个:

def calculate():
    df_behave['value'] = df_behave['value'].astype(int)
    for liste, new_df in df_behave.groupby(level=0):
        pi_df=pd.pivot_table(new_df, 'value', index=['date_time'], columns='field')
        print pi_df
    return pi_df

但两者都返回我 ValueError:long()的无效字面值为10: 'True'

推荐答案

尝试重置您的索引,将其设置为 date_time 字段,一个d然后拆开字段

Try resetting your index, setting it to list, date_time and field, and then unstacking field.

df.reset_index().set_index(['list', 'date_time', 'field']).unstack('field')

由于您的列似乎包含非数字数据,并且从上面的示例中,它只应包含整数,请尝试以下操作来查找错误的数据:

As your value column appears to contain non-numeric data, and from your examples above it should only contain integers, try the following to locate your bad data:

bad_rows = []
for n in range(len(df) - 1):
    if not isinstance(df.loc[n, 'value'], int):
        bad_rows.append(n)

你可能会先尝试强制这些值:

You may first want to try coercing the values:

df['value'] = df['value'].astype('int')

这篇关于大 pandas :多层数据帧中的数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆