简体   繁体   中英

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 .

 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 .

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


| 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?

One solution is to set a default value 0 and then use appy to a sliced 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.

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        |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM