简体   繁体   English

Appy/lambda 应用 function 到 dataframe 与其他列中的特定条件

[英]Appy/lambda apply function to dataframe with specific condition in other column

Problem问题

I have a data frame that looks like:我有一个看起来像这样的数据框:

p = {'parentId':['071cb2c2-d1be-4154-b6c7-a29728357ef3', 'a061e7d7-95d2-4812-87c1-24ec24fc2dd2', 'Highest Level', '071cb2c2-d1be-4154-b6c7-a29728357ef3'],
     'id_x': ['a061e7d7-95d2-4812-87c1-24ec24fc2dd2', 'd2b62e36-b243-43ac-8e45-ed3f269d50b2', '071cb2c2-d1be-4154-b6c7-a29728357ef3', 'a0e97b37-b9a1-4304-9769-b8c48cd9f184'],
    'type': ['c', 'c', 'c', 'r']}
df = pd.DataFrame(data = p)

df
|               parentId               |                 id_x                 | type   |
| ------------------------------------ | ------------------------------------ | ------ |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | c      |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | c      |
|              Highest Level           | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | c      |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | r      |

I created a function that counts the number of parentId that match a specific id_x .我创建了一个 function 来计算与特定id_x匹配的parentId的数量。

 def node_counter(id_x, parent_ID):
    counter = 0
    for child in parent_ID:
        if child == id_x:
            counter += 1
    return counter

df['Amount'] = df.apply(lambda x: node_counter(x['id_x'], df['parentId']), axis=1)

df

| parentId                             | id_x                                 | type | Amount |
| ------------------------------------ | ------------------------------------ | ---- | ------ |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | c    | 1      |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | c    | 0      |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | c    | 2      |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | r    | 0      |

Expected result预期结果

Now I want to create a new column Amount c with the same function, but only let it count if the type is c or r . Now I want to create a new column Amount c with the same function, but only let it count if the type is c or r .

The result should look like结果应该看起来像


| parentId                             | id_x                                 | type | Amount | Amount c |
| ------------------------------------ | ------------------------------------ | ---- | ------ | -------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | c    | 1      | 1        |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | c    | 0      | 0        |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | c    | 2      | 1        |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | r    | 0      | 0        |


or for r或用于r


| ParentId                             | id_x                                 | type | Amount | Amount r |
| ------------------------------------ | ------------------------------------ | ---- | ------ | -------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | c    | 1      | 0        |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | c    | 0      | 0        |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | c    | 2      | 1        |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | r    | 0      | 0        |

What I tried我试过的

I tried the following, but received a wrong result:我尝试了以下方法,但收到了错误的结果:


df['Amount C'] = df.apply(lambda x: node_counter(x['id_x'], df['parentId']) if (x['type'] == 'c') else 0, axis=1)
df

| ParentId                             | id_x                                 | type | Amount | Amount c |
| ------------------------------------ | ------------------------------------ | ---- | ------ | -------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | c    | 1      | 1        |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | c    | 0      | 0        |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | c    | 2      | 2        |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | r    | 0      | 0        |

How do I apply the if condition correctly in lambda/apply?如何在 lambda/apply 中正确应用 if 条件?

One solution is to set a default value 0 and then use appy to a sliced dataframe:一种解决方案是设置默认值 0,然后将 appy 用于切片 dataframe:

df['Amount C'] = 0  # set default value 0
mask_type = df['type'] == 'c'  # build index mask
df.loc[mask_type, 'Amount C'] = df.loc[mask_type].apply(lambda x: node_counter(x['id_x'], df['parentId']), axis=1)

I had to set the index mask also in the function for the parentId and it worked.我还必须在 function 中为parentId设置索引掩码,并且它有效。

df['Amount C'] = 0 # set default value 0
mask_type = df['type'] == 'c'  # build index mask
df.loc[mask_type,'Amount C'] = df.loc[mask_type].apply(lambda x: node_counter(x['id_x'], df.loc[mask_type,'parentId']), axis=1)

| parentId                             | id_x                                 | type | Amount | Amount c |
| ------------------------------------ | ------------------------------------ | ---- | ------ | -------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | c    | 1      | 1        |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | c    | 0      | 0        |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | c    | 2      | 1        |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | r    | 0      | 0        |

暂无
暂无

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

相关问题 应用函数以其他列为条件转换pandas数据框中的列 - Apply a function to translate a column in pandas dataframe with condition on other columns 如何通过使用特定于组的条件将lambda函数应用于Pandas DataFrame.group? - How to apply a lambda function to the Pandas DataFrame.groupby using a group-specific condition? 如何将 lambda 函数正确应用于数据框列? - How to apply lambda function to column of dataframe correctly? 如何根据数据框中的列条件应用函数 - How to apply function on the basis of column condition in a dataframe 使用 apply lambda function 根据另一列的条件创建一个新列 - Create a new column based on condition of another column with apply lambda function Pandas DataFrame将特定功能应用于每列 - Pandas DataFrame apply Specific Function to Each column pandas DataFrame,如何将函数应用于特定列? - pandas DataFrame, how to apply function to a specific column? pandas dataframe:无法应用 lambda 函数根据条件创建新列 if NaN or NA or \n or \t etc then 'No' else 'Yes' - pandas dataframe: failed to apply lambda function to create new column based on condition if NaN or NA or \n or \t etc then 'No' else 'Yes' 如何根据相邻列中的值将 lambda function 应用于特定列 - How to apply lambda function to specific column based on the values in the adjacent column 在 Pandas Z6A8064B5DF479455500553C47C50 上使用 apply 和 lambda function Z6A8064B5DF479455500553C47C50 - Using apply and lambda function on Pandas dataframe column that contains missing values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM