【发布时间】: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