【问题标题】:pandas groupby changes column into seriespandas groupby 将列更改为系列
【发布时间】:2021-02-01 17:04:21
【问题描述】:

df = sample.groupby('id')['user_id'].apply(list).reset_index(name='new') 这给了我:

    id       new
0   429     [659500]
1   1676    [2281394]
2   2389    [3973559]
3   2810    [4382598]
4   3104    [4733375]
5   3447    [5519461]
6   3818    [4453354]
7   3846    [4514870]
8   4283    [6378476]
9   4626    [6670089]
10  5022    [1116244]
11  5213    [6913646]
12  5899    [8213945, 8210403]
13  5962    [8733646]

但是 new 是一个系列,我怎样才能将“新”放入数据框中的字符串列表中

我试过df['new_id'] = df.loc[:, ['new']] 认为这至少可以解决我的系列问题...因为print(type(df.loc[:, ['new']])) 重新调整了数据框。

【问题讨论】:

    标签: python pandas dataframe pandas-groupby series


    【解决方案1】:

    试试这个:

    sample['new_id'] = sample['id'].map(sample.groupby('id')['user_id'].agg(list))
    

    【讨论】:

      猜你喜欢
      • 2014-10-06
      • 2019-08-03
      • 1970-01-01
      • 2017-01-12
      • 2021-05-24
      • 1970-01-01
      • 2020-01-15
      • 2020-10-19
      • 2017-04-06
      相关资源
      最近更新 更多