Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

pandas 分组合并元素后如何再重新组合。

原数据是这样的

res = [
    {"user_id": 1000, "log": ["0.3", "0.7", "1"]},
 {"user_id": 2000, "log": ["0.3", "0.7", "1"]},
 {"user_id": 1000, "log": ["0.3", "0.7", "1"]},
 {"user_id": 2000, "log": ["0.3", "0.7", "1"]}
]

统计log里的数值出现的次数再重新组合新的列表。

想要实现的效果如下,

# 统计到了分别出现了2次
res = [
    {"user_id": 1000,'0.3':2,'0.7':2,'1':2},
 {"user_id": 2000,'0.3':2,'0.7':2,'1':2},
]

我的代码,只实现了部分,还不太正确,后面没有太多思路。请教一下

x = pd.DataFrame(res)
res = x.groupby(['user_id'])['log'].apply(lambda x:np.concatenate(list(x))).reset_index()
x['log'] = res.apply(lambda x: dict(Counter(x['log'])),axis=1)
print(x)

输出

   user_id                           log
0     1000  {'0.3': 2, '0.7': 2, '1': 2}
1     2000  {'0.3': 2, '0.7': 2, '1': 2}
2     1000                           NaN
3     2000                           NaN

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

tmp = x.groupby(["user_id"])['log'].apply(lambda x: Counter(np.concatenate(list(x))))

res = tmp.unstack().reset_index()

res = res.apply(lambda x: x.to_dict(), axis=1)

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...