我有以下数据框:
df = pd.DataFrame([
[123, 'abc', '121'],
[124, 'abc', '121'],
[456, 'def', '121'],
[123, 'abc', '122'],
[123, 'abc', '122'],
[456, 'def', '145'],
[456, 'def', '145'],
[456, 'def', '146'],
], columns=['userid', 'name', 'dt'])
我已经按照日期分组了:
df2 = df.groupby('dt').apply(lambda df: df.reset_index(drop=True))
现在,我想调整以上内容,使它们采用以下格式:
userid name_1, name_2, ..., name_k
每个组,这样结束 df 看起来像这样:
userid name
123 abc
124 abc
456 def
123 abc, abc
最佳答案
您可以将 cumcount
与 pivot_table
一起使用,其中参数索引使用列 userid
和 dt
,因此看起来 create df2
不是必需的:
df['cols'] = 'name_' + (df.groupby(['userid','dt']).cumcount() + 1).astype(str)
print (df.pivot_table(index=['userid', 'dt'],columns='cols', values='name', aggfunc=''.join))
cols name_1 name_2
userid dt
123 121 abc None
122 abc abc
124 121 abc None
456 121 def None
145 def def
146 def None
关于python - 在 Pandas 中旋转一个 groupby 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38735384/