我有以下dataframe ...

idx Group      key     value  Time     IsTrue  
1   bicycle    person  yes    9:30      yes         
2   bicycle    name    bob    9:30      yes         
3   bicycle    alive   yes    9:30      yes            
5   non-cycle  person  no     1:30      no      
6   non-cycle  name    jack   1:30      no               

我想从dataframe以下结果

idx Group       Time  IsTrue person name  alive
1   bicycle     9:30  yes    yes    bob   yes
2   non-cycle   1:30  no     no     jack  NA

其中关键的列将成为新的列和值是这些新列的行。所有其他行具有相同总是有除键和值列中的值相同。该键可以改变,所以我去的东西是动态的。

我目前的解决方案使用pandas GROUPBY和应用(基于集团列),并为每个组创建一个新的dataframe,但似乎大大超过设计。任何简单的解决方案呢?

分析解答

你期望的输出是混乱的。让我们试试这个解决方案,如果这是你想要的。如果不是,我会删除它

df.pivot_table(index=['Group', 'Time', 'IsTrue'], columns='key', values='value', aggfunc='first').reset_index()

Out[487]:
key      Group  Time IsTrue alive  name person
0      bicycle  9:30    yes   yes   bob    yes
1    non-cycle  1:30     no   NaN  jack     no