【发布时间】:2018-07-08 13:55:30
【问题描述】:
我有一个 pandas 数据集,其中有一列是以逗号分隔的字符串,例如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)
我想获得topics 中每个值的计数和平均分。所以:
topic_id,count,mean
1,2,5
11,2,8
12,1,6
等等。我该怎么做?
我已经知道了:
df['topic_ids'] = df.topics.str.split()
但现在我想我想把topic_ids 炸开,所以整个值集中的每个唯一值都有一列...?
【问题讨论】:
-
平均分数是指
df.topics.str.split(',',expand=True).astype(int).mean(axis=1)?