简体   繁体   English

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

[英]How to update a column value in a row for specific value in another column in pandas dataframe?

I have a dataframe with 3 columns id , type , type_value .我有一个包含 3 列idtypetype_value的数据type_value

They're already populated with values.它们已经填充了值。 type column contains repeating values type1 , type2 , type3 , type4 . type列包含重复值type1 , type2 , type3 , type4

type_value contains a Counter object which has frequencies of occurrences in it. type_value包含一个 Counter 对象,该对象具有出现频率。

All I need is to perform some operations on the Counter dictionary and update the type_value column when the type is matched with type1 and leave the other rows untouched.我所需要的只是对 Counter 字典执行一些操作,并在typetype1匹配时更新type_value列,并保持其他行不变。

Input:输入:

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

Output:输出:

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

Fitler rows by condition and apply your custom function: Fitler 按条件行并应用您的自定义函数:

def func(x):
    #your operation
    return x

mask = df['type'] == 'type1'
df.loc[mask, 'type_value'] = df.loc[mask, 'type_value'].apply(func)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取熊猫数据框中特定行和列的值 - get a value of a specific row and column in pandas dataframe 如何在 pandas dataframe 的特定行和列中插入输入值 - How to insert a value from an input in a specific row and column in a pandas dataframe Pandas Python 用另一个行值更新列 - Pandas Python update column with another row value 在Pandas中,如何根据另一行中的另一列值更新一行中的列值 - In Pandas how to update column value in one row based on another column value in another row 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 如何查找两个 pandas dataframe 并从另一个 dataframe 列更新第一个 dataframe 列中的值? - how to do lookup on two pandas dataframe and update its value in first dataframe column from another dataframe column? 对于Pandas数据框中的每一行,确定另一列中是否存在一列值 - For every row in Pandas dataframe determine if a column value exists in another column Label 基于另一列(同一行)的值的列 pandas dataframe - Label a column based on the value of another column (same row) in pandas dataframe 使用 Pandas 将特定列值替换为另一个数据框列值 - Replace specific column values with another dataframe column value using Pandas (行、列):值到 Pandas DataFrame - (Row, Column) : Value to Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM