Python Pandas Groupby/Append columns

て烟熏妆下的殇ゞ 提交于 2021-01-28 19:30:43

问题


This is my example dataframe:

    Index  Param1 Param2
    A      1      2
    A      3      4   
    B      1      3
    B      4      Nan
    C      2      4

What I would like to get is:

    Index  Param1 Param2 Param3 Param4
    A      1      2      3      4
    B      1      3      4
    C      2      4

What would be the best way to achieve it using pandas? Thanks in advance for your help.


回答1:


You can use groupby with unstack:

def f(x):
    return (pd.DataFrame(np.sort(x.values.ravel())))

df = df.groupby('Index')['Param1','Param2'].apply(f).unstack()
df.columns = df.columns.droplevel(0)
print (df)
       0  1     2     3
Index                  
A      1  2     3     4
B      1  3     4   Nan
C      2  4  None  None

because if use Series get:

TypeError: Series.name must be a hashable type

Another solution with cumcount:

df = df.set_index('Index').stack().reset_index(name='vals')
df['g'] = 'Param' + df.groupby('Index').cumcount().add(1).astype(str)
df = df.pivot(index='Index', columns='g', values='vals')
print (df)
g      Param1  Param2  Param3  Param4
Index                                
A         1.0     2.0     3.0     4.0
B         1.0     3.0     4.0     NaN
C         2.0     4.0     NaN     NaN



回答2:


import numpy as np
import pandas as pd

df = pd.DataFrame({'Index': ['A', 'A', 'B', 'B', 'C'], 'Param1': [1, 3, 1, 4, 2],
                   'Param2': [2, 4, 3, np.nan, 4]}).set_index('Index')
print(df)

#        Param1  Param2
# Index                
# A           1     2.0
# A           3     4.0
# B           1     3.0
# B           4     NaN
# C           2     4.0

def fn(g):
    return pd.Series(g.values.ravel())

res = df.groupby(df.index).apply(fn).unstack()
res.columns = ['Param1', 'Param2', 'Param3', 'Param4']
print(res)

#        Param1  Param2  Param3  Param4
# Index                                
# A         1.0     2.0     3.0     4.0
# B         1.0     3.0     4.0     NaN
# C         2.0     4.0     NaN     NaN


来源:https://stackoverflow.com/questions/40788177/python-pandas-groupby-append-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!