如何为熊猫数据框中的另一列中的特定值更新一行中的列值?

时间:2019-08-16 05:21:46

标签: python pandas dataframe counter pandas-groupby

我有一个包含3列idtypetype_value的数据框。

它们已经填充了值。 type列包含重复值type1type2type3type4

type_value包含一个Counter对象,该对象中的出现频率很高。

我需要做的是在Counter字典上执行一些操作,并在type_value type 匹配时更新type1列,并保留其他行保持不变。

输入:

id  type    type_value
1   type1   Counter object that needs to be updated
2   type2   some random value
3   type3   some random value
4   type4   some random value
5   type1   Counter object that needs to be updated
6   type2   some random value
7   type3   some random value
8   type4   some random value
9   type1   Counter object that needs to be updated
10  type2   some random value
11  type3   some random value
12  type4   some random value
13  type1   Counter object that needs to be updated
14  type2   some random value
15  type3   some random value
16  type4   some random value

输出:

id  type    type_value
1   type1   Do an operation on this and update with new values
2   type2   some random value
3   type3   some random value
4   type4   some random value
5   type1   Do an operation on this and update with new values
6   type2   some random value
7   type3   some random value
8   type4   some random value
9   type1   Do an operation on this and update with new values
10  type2   some random value
11  type3   some random value
12  type4   some random value
13  type1   Do an operation on this and update with new values
14  type2   some random value
15  type3   some random value
16  type4   some random value

1 个答案:

答案 0 :(得分:1)

按条件拟合行并应用您的自定义函数:

{{1}}