python - 两个 Pandas Series 的字符串匹配
问题描述
我在两个不同的数据帧中有两个(地址)列,每列都有不同的长度,我希望从数据帧的一列到另一个数据帧的另一列迭代每个元素。意思是,我希望检查第一个数据帧第一列中的每个元素是否与第二个数据帧第二列的任何元素匹配并返回一个布尔值。
如何在 python 中实现上述内容?
数据框 1:
0 New Delhi, India
1 Mumbai, India
2 Bangalore, India
3 Dwarka, New Delhi, India
数据框 2:
0 Nepal
1 Assam, India
2 Delhi
结果:(长度应等于 df 1 的 col 1 的 len)
True
False
False
True
解决方案
import pandas as pd
sales1 = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': 140},
{'account': 'Alpha Co', 'Jan': 200, 'Feb': 210, 'Mar': 215},
{'account': 'Blue Inc', 'Jan': 50, 'Feb': 90, 'Mar': 95 }]
sales2 = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': 140},
{'account': 'A', 'Jan': 200, 'Feb': 210, 'Mar': 215},
{'account': 'S', 'Jan': 50, 'Feb': 90, 'Mar': 95 }]
df1 = pd.DataFrame(sales1)
df2 = pd.DataFrame(sales2)
def CheckDF(df1,df2):
for (item, Value),(item1, Value1) in
zip(df1['account'].iteritems(),df2['account'].iteritems()):
if len(str(Value).strip()) == len(str(Value1).strip()):
print(True)
else:
print(False)
CheckDF(df1,df2)
DF1:
Feb Jan Mar account
0 200 150 140 Jones LLC
1 210 200 215 Alpha Co
2 90 50 95 Blue Inc
DF2:
Feb Jan Mar account
0 200 150 140 Jones LLC
1 210 200 215 A
2 90 50 95 S
输出:
True
False
False
推荐阅读
- ruby-on-rails - 在引导模式中显示视图不加载数据
- python - 如何动态选择我在 Python 中打印的成员变量?
- javascript - Discord Collector 不收集 DM
- javascript - HTML按钮onclick属性不调用该函数,更改时行为奇怪
- python - 调用 __init__.py 中的模块与 python setup.py install 中断;导入 init 的模块依赖于尚未构建的扩展
- python - Beautifulsoup Python Youtube Scrape 不工作
- java - Kotlin/Java - 如何识别内部包?
- reactjs - 如何在不继续代码的情况下解决承诺
- sql - Google BigQuery 分析函数 - MAX(FUNCTION)
- c# - 如何在 linerenderer 的两条连接线的每个角上绘制/显示文本?