gpt4 book ai didi

python - 如何检索关键子串并按此子串计数?

转载 作者:行者123 更新时间:2023-11-28 22:09:49 25 4
gpt4 key购买 nike

我在 Python 中有以下字典:

OrderedDict([('data(xxx_a1)_first_type', 0.12),
('data(xxx_a2)_first_type', 0.14),
('test(xx_b15)_second_type', 0.15)])

有什么办法统计first_typesecond_type,并计算每种类型的平均值吗?

预期结果:

type         avg_val
first_type 0.13
second_type 0.15

最佳答案

import pandas as pd
list_Tuples = [(z, np.mean([y for x,y in v.items() if x.endswith(z)]), len([y for x,y in v.items() if x.endswith(z)])) for z in ['first_type', 'second_type']]
pd.DataFrame(list_Tuples, columns=['type', 'avg_val', 'count'])

输出:

    type         avg_val  count
0 first_type 0.13 2
1 second_type 0.15 1

其中 v 是数据。

关于python - 如何检索关键子串并按此子串计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com