【发布时间】:2022-11-08 05:19:39
【问题描述】:
示例 df 如下所示:
Price Group
10 apple
8 apple
7 apple
6 apple
10 apple
12 berry
11 berry
11 berry
7 berry
9 berry
每个键都有一系列值的嵌套字典:
apple:{'A':[9, 10], 'B':[6, 8], 'C':[3-5]}
berry:{'A':[11, 12], 'B':[6, 9]}
如果Price 在每个键的值范围内,则将Cat 列添加到df,每个Group 中都有相应的键。输出:
Price Group Cat
10 apple A
8 apple B
7 apple B
6 apple B
10 apple A
12 berry A
11 berry A
11 berry A
7 berry B
9 berry B
这可以使用 python 中的任何内置函数或使用包含 df 和 dicts 的函数的任何其他方式来实现吗?
【问题讨论】:
标签: python dictionary