【发布时间】:2021-04-17 08:50:20
【问题描述】:
问题
我有一个看起来像这样的数据框:
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 |
我创建了一个函数来计算与特定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 |
预期结果
现在我想创建一个具有相同功能的新列Amount c,但仅在type 是c 或r 时才计算。
结果应该是这样的
| 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 |
或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 |
我尝试了什么
我尝试了以下方法,但收到了错误的结果:
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 |
如何在 lambda/apply 中正确应用 if 条件?
【问题讨论】:
-
用必要的细节编辑上一个问题,而不是posting your question multiple times。
-
@aneriod - 这是问题的另一方面。这就是我创建一个新问题的原因。
标签: python pandas function lambda