在数据框中查找包含字符串作为值的行的索引?

2024-04-19 21:35:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧df和df1,从这里我比较的值。对代码的确切期望是:

1)遍历数据帧df,当满足一定条件时 条件下,它将值保存在变量“value”的第10列中。 2) 它调用函数GDN_func,然后在数据帧df1中, 它遍历列“B”,即第二列,并比较其 “items”返回值,如果匹配,则返回row_index。你知道吗

我已将两个excel表转换为数据框。 row_index=df.index.get_loc(item)不会给出结果,因为变量存储为字符串。你知道吗

这里的代码只是一部分。你知道吗

我附上了我的两个数据帧和代码片段。你知道吗

def GDN_func():
#defining GDN_func
    for item in (df1.iloc[:, 1]):
        if(item==value):
            p=df1[1].values.tolist().index(item)
            print(p) # This is not working

for row in range(0, mr-1):
    if((df.loc[row]['ATC']=='All') &
       (df.loc[row]['Data_Grain']=='Leadership Capability')):
        value=df.loc[row][10]
        GDN_func()
    else:
        print("These are different rows")

Tags: 数据代码indfforindexifvalue