简体   繁体   English

pandas groupby 将列更改为系列

[英]pandas groupby changes column into series

df = sample.groupby('id')['user_id'].apply(list).reset_index(name='new') this gives me: 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]

However new is a series, how can I get 'new' into a list of strings in a dataframe ?然而new是一个系列,我怎样才能在 dataframe 中的字符串列表中获得“新”

I've tried df['new_id'] = df.loc[:, ['new']] thinking that this would at least solve my series issue... since print(type(df.loc[:, ['new']])) retuns a dataframe.我试过df['new_id'] = df.loc[:, ['new']]认为这至少可以解决我的系列问题......因为print(type(df.loc[:, ['new']]))返回 dataframe。

Try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM