Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: split string, and count values? [duplicate]

Tags:

python

pandas

I've got a pandas dataset with a column that's a comma-separated string, e.g. 1,2,3,10:

data = [
  { 'id': 1, 'score': 9, 'topics': '11,22,30' },
  { 'id': 2, 'score': 7, 'topics': '11,18,30' },
  { 'id': 3, 'score': 6, 'topics': '1,12,30' },
  { 'id': 4, 'score': 4, 'topics': '1,18,30' }
]
df = pd.DataFrame(data)

I'd like to get a count and a mean score for each value in topics. So:

topic_id,count,mean
1,2,5
11,2,8
12,1,6

et cetera. How can I do this?

I've got as far as:

df['topic_ids'] = df.topics.str.split()

But now I guess I want to explode topic_ids out, so there's a column for each unique value in the entire set of values...?

like image 373
Richard Avatar asked Jul 12 '26 09:07

Richard


2 Answers

unnest then groupby and agg

df.topics=df.topics.str.split(',')
New_df=pd.DataFrame({'topics':np.concatenate(df.topics.values),'id':df.id.repeat(df.topics.apply(len)),'score':df.score.repeat(df.topics.apply(len))})

New_df.groupby('topics').score.agg(['count','mean'])

Out[1256]: 
        count  mean
topics             
1           2   5.0
11          2   8.0
12          1   6.0
18          2   5.5
22          1   9.0
30          4   6.5
like image 196
BENY Avatar answered Jul 15 '26 15:07

BENY


In [111]: def mean1(x): return np.array(x).astype(int).mean()

In [112]: df.topics.str.split(',', expand=False).agg([mean1, len])
Out[112]:
       mean1  len
0  21.000000       3
1  19.666667       3
2  14.333333       3
3  16.333333       3
like image 27
MaxU - stop WAR against UA Avatar answered Jul 15 '26 13:07

MaxU - stop WAR against UA