Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 按ID分组,同时保留所有数据。python熊猫_Python 3.x_Pandas_Pandas Groupby - Fatal编程技术网

Python 3.x 按ID分组,同时保留所有数据。python熊猫

Python 3.x 按ID分组,同时保留所有数据。python熊猫,python-3.x,pandas,pandas-groupby,Python 3.x,Pandas,Pandas Groupby,我有以下数据: UserId Date Action 1 01/01/2018 One 1 02/01/2018 One 1 03/01/2018 Two 1 05/01/2018 Two 2 01/01/2018 One 2 03/01/2018 Three 2 07/01/2018 One 3 04/01/2018

我有以下数据:

UserId    Date         Action
1       01/01/2018     One
1       02/01/2018     One
1       03/01/2018     Two
1       05/01/2018     Two
2       01/01/2018     One
2       03/01/2018     Three
2       07/01/2018     One
3       04/01/2018     One
我想得到:

UserId      Date_1      Action_1     Date_2       Action_2  (...)  
1         01/01/2018      One      02/01/2018       One
2         01/01/2018      Two      03/01/2018      Three
3         04/01/2018      One        NULL           NULL
有什么提示吗?

使用

d = df.set_index(
    ['UserId', df.groupby(['UserId']).cumcount().add(1).astype(str)]
).unstack().sort_index(axis=1, level=1)

d.columns = d.columns.map('_'.join)
d

            Date_1 Action_1      Date_2 Action_2      Date_3 Action_3      Date_4 Action_4
UserId                                                                                    
1       01/01/2018      One  02/01/2018      One  03/01/2018      Two  05/01/2018      Two
2       01/01/2018      One  03/01/2018    Three  07/01/2018      One        None     None
3       04/01/2018      One        None     None        None     None        None     None
s = pd.concat([g.reset_index().unstack() for _, g in df.groupby(['UserId'])], 1)
s = s.reset_index(level=0).T.sort_index(axis=1)
s.columns = s.loc['level_0'] + '_' + s.columns.astype(str)
s = s.iloc[1:,:]