使用熊猫将数据帧从长到宽转换-单行输出

时间:2019-09-25 10:24:01

标签: python python-3.x pandas dataframe transformation

我有一个如下所示的数据框

df = pd.DataFrame({
'subject_id':[1,1,1,1,2,2,2,2],
'date':['2173/04/11','2173/04/12','2173/04/11','2173/04/12','2173/05/14','2173/05/15','2173/05/14','2173/05/15'],
'time_1':['2173/04/11 12:35:00','2173/04/12 12:50:00','2173/04/11 12:59:00','2173/04/12 13:14:00','2173/05/14 13:37:00','2173/05/15 13:39:00','2173/05/14 18:37:00','2173/05/15 19:39:00'],
 'val' :[5,5,40,40,7,7,38,38],
 'iid' :[12,12,12,12,21,21,21,21]   
 })
df['time_1'] = pd.to_datetime(df['time_1'])
df['day'] = df['time_1'].dt.day

我尝试使用stack,unstack,pivot and melt方法,但似乎无济于事

pd.melt(df, id_vars =['subject_id','val'], value_vars =['date','val']) #1

df.unstack().reset_index()  #2

df.pivot(index='subject_id', columns='time_1', values='val')  #3 

我希望我的输出数据帧如下图所示

enter image description here

更新的屏幕截图

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

想法是由GroupBy.cumcount创建具有相同索引的列/列的帮助器系列-在此处subject_id,创建MultiIndex,由DataFrame.unstack整形,最后展平{{1 }}:

MulitIndex in columns

如果没有相同数量的cols = ['time_1','val'] df = df.set_index(['subject_id', df.groupby('subject_id').cumcount().add(1)])[cols].unstack() df.columns = [f'{a}{b}' for a, b in df.columns] df = df.reset_index() print (df) subject_id time_11 time_12 time_13 \ 0 1 2173-04-11 12:35:00 2173-04-12 12:50:00 2173-04-11 12:59:00 1 2 2173-05-14 13:37:00 2173-05-15 13:39:00 2173-05-14 18:37:00 time_14 val1 val2 val3 val4 0 2173-04-12 13:14:00 5 5 40 40 1 2173-05-15 19:39:00 7 7 38 38 组,则期望缺少值-id使用最大计数,然后添加misisng值:

unstack

df = pd.DataFrame({
'subject_id':[1,1,1,2,2,3],
'date':['2173/04/11','2173/04/12','2173/04/11','2173/04/12','2173/05/14','2173/05/15'],
'time_1':['2173/04/11 12:35:00','2173/04/12 12:50:00','2173/04/11 12:59:00',
          '2173/04/12 13:14:00','2173/05/14 13:37:00','2173/05/15 13:39:00'],
 'val' :[5,5,40,40,7,7],
 'iid' :[12,12,12,12,21,21]   
 })
df['time_1'] = pd.to_datetime(df['time_1'])
df['day'] = df['time_1'].dt.day
print (df)
   subject_id        date              time_1  val  iid  day
0           1  2173/04/11 2173-04-11 12:35:00    5   12   11
1           1  2173/04/12 2173-04-12 12:50:00    5   12   12
2           1  2173/04/11 2173-04-11 12:59:00   40   12   11
3           2  2173/04/12 2173-04-12 13:14:00   40   12   12
4           2  2173/05/14 2173-05-14 13:37:00    7   21   14
5           3  2173/05/15 2173-05-15 13:39:00    7   21   15