繁体   English   中英

在 python 中从列表到 dataframe 的列名中查找相似的匹配项

[英]find similar match from list to column names of a dataframe in python

我有一个“净金额”的可能列名列表,即

list1 = ['total amount', 'total cash', 'net amount']

我有一个 dataframe 例如其列名是

df.columns = ['accounts receivables ffa', 'net amount of the year', 'cash refunded', 'payement']

我想将list1'net amount'可能名称与df匹配,它应该获取“年度净额”

list1df.columns匹配,并从 df 中获取列名的相似匹配

请问有什么建议吗?

提前致谢

你可以使用https://pypi.org/project/pyjarowinkler/

from pyjarowinkler import distance
import pandas as pd 

df = pd.DataFrame( [], columns=['accounts receivables ffa', 'net amount of the year', 'cash refunded', 'payement'])
lst1 = ['total amount', 'total cash', 'net amount']
    
for item in  lst1:
    for col in df.columns:
        if distance.get_jaro_distance(item,col) >0.85:
            print(item,";",col)

在此处输入图像描述

如何遍历列表和列名。 然后检查列表项(字符串)是否为列名的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ(也是字符串)。

for el in list1:
    for col_name in df.columns:
        if el in col_name:
            print(col_name)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM