提问人:JohnRambo 提问时间:11/14/2023 最后编辑:JohnRambo 更新时间:11/17/2023 访问量:55
Python Pandas:确认 DataFrame A 的子字符串是否是 DataFrame B 中字符串的一部分
Python Pandas: Confirm if a substring of DataFrame A is part of a string in DataFrame B
问:
我有两个数据帧:
import pandas as pd
countries_list_a = pd.DataFrame({'Country' : ['Australia', 'United Kingdom', 'United States'], 'Code' : ['A', 'B', 'C']})
countries_list_b = pd.DataFrame ({'Name' : ['Jack', 'Maria', 'David'], 'Geo' : ['New York, United States', 'Sydney, Australia', 'London, United Kingdom']})
并且我想迭代检查特定列的每个单元格中的字符串是否可以在特定列的任何单元格中找到。countries_list_a
countries_list_b
更具体地说,这些是列:
countries_list_a["Country"]
countries_list_b["Geo"]
并且我想迭代检查每个单元格中的字符串是否可以
在 的任何单元格中发现。需要注意的是,我需要跟踪索引并将它们分别存储在数组中。如果我尝试像这样手动验证字符串的存在:countries_list_a["Country"]
countries_list_b["Geo"]
tmp_country_a = countries_list_a["Country"][0]
tmp_country_b = countries_list_b["Geo"][1]
if (tmp_country_a in tmp_country_b):
print ("Correct")
一切正常。当然,手动执行此过程不是一种选择。
如果我尝试构建这样的循环:
index_a = np.size(countries_list_a["Country"])
index_b = np.size(countries_list_b["Geo"])
for i in range(index_a):
tmp_country_a = countries_list_a["Country"][i]
for j in range(index_b):
tmp_country_b = countries_list_b["Geo"][j]
if (tmp_country_a in tmp_country_b):
print("Correct")
我收到以下错误:
TypeError: argument of type 'int' is not iterable
有什么想法吗?提前致谢
答:
0赞
BERA
11/17/2023
#1
import pandas as pd
countries_list_a = pd.DataFrame({'Country' : ['Australia', 'United Kingdom', 'United States'], 'Code' : ['A', 'B', 'C']})
# Country Code
# Australia A
# United Kingdom B
# United States C
countries_list_b = pd.DataFrame ({'Name' : ['Jack', 'Maria', 'David'], 'Geo' : ['New York, United States', 'Sydney, Australia', 'London, United Kingdom']})
# Name Geo
# Jack New York, United States
# Maria Sydney, Australia
# David London, United Kingdom
geo = countries_list_b.Geo
countries_list_a["country_found"] = [any(country in g for g in geo) for country in countries_list_a.Country]
# print(countries_list_a)
# Country Code country_found
# Australia A True
# United Kingdom B True
# United States C True
评论
,
countries_list_b['Geo']
merge
'Orlando, FL, United States'
Idaho, Moscow, United States of America