【问题标题】:Find if a value in a columns is in another column查找列中的值是否在另一列中
【发布时间】:2020-12-13 23:33:16
【问题描述】:

我有住房地址和邮政编码数据,我想查看它是否存在于另一个表的另一列中。

我有两个 Excel 表,我上传到 Python,表 A 和表 B。我想从表 A 的第 1 列 (house_address) 和第 2 列 (zip) 中获取每个值,并使用 for 循环查看是否值在表 B 的第 1 列(full_address)中。

表 A:

house_address zip
124 HOUSE LANE 12345
123 home ln 54321

表 B:

house_address zip property_size full_address
124 HOUSE LANE 12345 1000 124 HOUSE LANE 12345
123 home ln 54321 2000 123 home ln 54321
987 strawberry rd 11111 3000 987 strawberry rd 11111

我的代码是:

# upload tables
table_a = pd.read_excel('table_a.xlsx')
table_b = pd.read_excel('table_b.xlsx')

# create a list from table b and table b of the full_address column for matching purposes
full_address_table_b = list(table_b['full_address'])


# for loop checks if the address is already in my table_B. If it is return the information to # the end user. If it is not then move on to the else statement which will do something else
for i in range(len(table_a)):

    
    # create a concat list of table a full address
    full_address_table_a = str(table_a['house_address']) + " " + str(table_b['zip'])

    # check if the address is already in our dataset
    if table_b.loc[table_b['full_address'].str.contains(full_address_table_a, case=False)]:

        # if it is, then just print the info from table b
        print(housing_df[housing_df['full_address'] == full_address_test])

    # else run another piece of code
    else:
        print("run this part of the code")

当我运行上面的代码时,我得到以下错误:

ValueError:DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

我想知道是否有人知道我在这里做错了什么。也可能还有一种更有效的方法来创建上述代码,因此欢迎提出更高效代码的建议。

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    如果问题的严重程度,您可以简单地合并它们以查看哪些值是常见的:

    table_a.merge(table_b, on = ['house_address', 'zip'])
    

    结果:

    house_address   zip property_size   full_address
    0   124 HOUSE LANE  12345   1000    124 HOUSE LANE 12345
    1   123 home ln 54321   2000    123 home ln 54321
    

    如果您想具体查看哪些仅在 table_atable_b 或两者中,请传递一个指标,然后根据需要过滤掉:

    table_a.merge(table_b, on = ['house_address', 'zip'], how = 'outer', indicator = True)
    

    结果:

    house_address   zip property_size   full_address    _merge
    0   124 HOUSE LANE  12345   1000    124 HOUSE LANE 12345    both
    1   123 home ln 54321   2000    123 home ln 54321   both
    2   987 strawberry rd   11111   3000    987 strawberry rd 11111 right_only
    

    【讨论】:

    • 我认为这是一种解决方案,但我使用 for 循环的原因是因为我正在构建一些东西,它会让用户输入一个 Excel 表地址,并希望在所有房屋的信息。 for 循环将允许查看地址是否已经在主数据集中(如果是,它将只从那里获取数据。如果不是,将调用 API 来获取数据)。
    【解决方案2】:

    如果你不想使用for 循环,你可以这样做:

    df1['full_address'] = df1['house_address'].astype(str) + " " df1['zip'].astype(str)
    first_list = df1['house_address'].unique().tolist()
    second_list = df2['house_address'].unique().tolist()
    
    common_houses = [house for house in first_list if house in second_list] 
    

    那么要查看2之间全地址匹配的完整数据,可以这样做:

    common_houses_df = df2[df2['full_address'].isin(common_houses)]
    

    这将为您提供包含 2 个文件中的公共地址的完整数据集。

    【讨论】:

    • 我认为这是一种解决方案,但我使用 for 循环的原因是因为我正在构建一些东西,它会让用户输入一个 Excel 表地址,并希望在所有房屋的信息。 for 循环将允许查看地址是否已经在主数据集中(如果是,它将只从那里获取数据。如果不是,将调用 API 来获取数据)。
    猜你喜欢
    • 1970-01-01
    • 2022-08-26
    • 2014-01-30
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多