将行转置为列,同时根据组展平数据框

时间:2019-09-25 02:14:13

标签: python pandas 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               

我想从数据框中获得以下结果

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&apply(基于“组”列),并为每个组创建一个新的数据框,但这似乎超出了设计水平。有没有更简单的解决方案?

2 个答案:

答案 0 :(得分:3)

修改
当您固定输出时。我使用set_indexunstack

添加了另一个解决方案
df.set_index(['Group', 'Time', 'IsTrue', 'key'])['value'].unstack().reset_index()

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

原始
您所需的输出令人困惑。如果需要的话,请尝试此解决方案。如果不是,我将其删除

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

答案 1 :(得分:1)

IN:

df = pd.read_clipboard()

pivot = df[['key', 'value', 'Group']].pivot(index='Group',columns='key').droplevel(0, axis=1).reset_index()
df.drop(['idx','key', 'value'], axis=1, inplace=True)

df = df.merge(pivot, on='Group').drop_duplicates().reset_index(drop=True)

OUT:

|   | Group     | Time | IsTrue | alive | name | person |
|---|-----------|------|--------|-------|------|--------|
| 0 | bicycle   | 9:30 | yes    | yes   | bob  | yes    |
| 1 | non-cycle | 1:30 | no     | NaN   | jack | no     |