我如何遍历 Pandas df 中每一行的值来检查该值是否满足条件
How did I loop through each row's value in Pandas df to check if that value satisfies a condition
我有一个table如下:
ID IsCreated ParentID
101 0 NA
102 1 101
103 0 NA
104 1 102
105 1 104
我想添加一个名为 'OGParentID' 的新列,结果如下:
ID IsCreated ParentID OGParentID
101 0 NA 101
102 1 101 101
103 0 NA 103
104 1 102 101
105 1 104 101
这里使用的逻辑如下:
对于每一行,检查是否 IsCreated = 1,如果是,则在 ID 列中查找 ParentID 以检查它的 IsCreated 是否为 0,如果是,则将 OGParentID 设置为查找的父 ID,否则,检查查找的 ParentID 的父 ID 和继续循环。
您可以编写一个函数,然后将其应用于列
def lookup(parent_id):
while True:
# find the row where ID is equal to the parent_id
temp = df[df[‘ID’]==parent_id]
# check if iscreated is 0, return the parent_id if it is 0
if temp[‘IsCreated’][0]==0:
return parent_id
# at this point, iscreated must be 1, so now we set parent_id to the ParentID in this row and do it again until we find iscreated is 0.
parent_id = temp[‘ParentID’]
df[‘OGParentID’] = df[‘ParentID’].apply(lookup)
此代码将获取每个 ParentID 并将其提供给函数 lookup
。
函数 lookup
根据 OP 的要求执行以下操作:
- 查找 ID 等于给定 parent_id
的行
- 检查iscreated是否为0。如果是,return该行的ParentID
- 如果不是,则将parent_id设置为该行的ParentID并重复步骤1
你可以这样做:
#creating a dictionary "x" to have all the unique parent-child relationships using comprehension
x={i:int(j) for i,j in zip(df['ID'],df['ParentID'].fillna(df['ID']))}
#assigning ID to the new column
df['OGParentID']=df['ID']
#loop to replace the dict items ,i.e, replacing all the childs with parents
#counter is limited to the length of unique elements in dict
i=0
while i<=len(x):
df['OGParentID']=df['OGParentID'].replace(x)
i+=1
Output:
ID IsCreated ParentID OGParentID
0 101 0 NaN 101
1 102 1 101.0 101
2 103 0 NaN 103
3 104 1 102.0 101
4 105 1 104.0 101
我有一个table如下:
ID IsCreated ParentID
101 0 NA
102 1 101
103 0 NA
104 1 102
105 1 104
我想添加一个名为 'OGParentID' 的新列,结果如下:
ID IsCreated ParentID OGParentID
101 0 NA 101
102 1 101 101
103 0 NA 103
104 1 102 101
105 1 104 101
这里使用的逻辑如下: 对于每一行,检查是否 IsCreated = 1,如果是,则在 ID 列中查找 ParentID 以检查它的 IsCreated 是否为 0,如果是,则将 OGParentID 设置为查找的父 ID,否则,检查查找的 ParentID 的父 ID 和继续循环。
您可以编写一个函数,然后将其应用于列
def lookup(parent_id):
while True:
# find the row where ID is equal to the parent_id
temp = df[df[‘ID’]==parent_id]
# check if iscreated is 0, return the parent_id if it is 0
if temp[‘IsCreated’][0]==0:
return parent_id
# at this point, iscreated must be 1, so now we set parent_id to the ParentID in this row and do it again until we find iscreated is 0.
parent_id = temp[‘ParentID’]
df[‘OGParentID’] = df[‘ParentID’].apply(lookup)
此代码将获取每个 ParentID 并将其提供给函数 lookup
。
函数 lookup
根据 OP 的要求执行以下操作:
- 查找 ID 等于给定 parent_id 的行
- 检查iscreated是否为0。如果是,return该行的ParentID
- 如果不是,则将parent_id设置为该行的ParentID并重复步骤1
你可以这样做:
#creating a dictionary "x" to have all the unique parent-child relationships using comprehension
x={i:int(j) for i,j in zip(df['ID'],df['ParentID'].fillna(df['ID']))}
#assigning ID to the new column
df['OGParentID']=df['ID']
#loop to replace the dict items ,i.e, replacing all the childs with parents
#counter is limited to the length of unique elements in dict
i=0
while i<=len(x):
df['OGParentID']=df['OGParentID'].replace(x)
i+=1
Output:
ID IsCreated ParentID OGParentID
0 101 0 NaN 101
1 102 1 101.0 101
2 103 0 NaN 103
3 104 1 102.0 101
4 105 1 104.0 101