python - 在 Pandas 中旋转一个 groupby 对象?

标签 python pandas

我有以下数据框:

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))

现在,数据框看起来像这样: enter image description here

现在,我想调整以上内容,使它们采用以下格式: userid name_1, name_2, ..., name_k 每个组,这样结束 df 看起来像这样:

userid   name
123      abc
124      abc
456      def
123      abc, abc

最佳答案

您可以将 cumcountpivot_table 一起使用,其中参数索引使用列 useriddt,因此看起来 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/

相关文章:

python - 从 Pandas 数据框中的字符串中删除数字

python - 在 Pandas 中使用 DataFrame.ix 和元组索引

python - 在 Python 中填写动态在线表单

Python,Kivy, "AssertionError: None is not callable"按钮调用函数时出错

Python列表操作: Given a list of ranges number,返回组合范围的列表

python - 使用python根据经度和纬度匹配两个数据集

str(x) 的 Python 默认行为

python - 如何使用 BeautifulSoup 获取 child 标签的描述文本

python - 通过 xlwings 从 pandas 到 excel - 不要存放索引

python - Pandas:用具有相同固定重复次数的数据帧填充固定数量的新列