简体   繁体   English

使用dict的值过滤多列上的pandas数据框以实现部分字符串匹配

[英]Filter a pandas dataframe on multiple columns for partial string match, using values from a dict

I need to filter a dataframe on multiple values from a dict 我需要根据字典中的多个值过滤数据框

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
filters_raw = {'continent': {'filterTerm': 'Asi', 'column': {'rowType': 'filter', 'key': 'continent', 'name': 'continent', 'editable': True, 'sortable': True, 'resizable': True, 'filterable': True, 'width': 147, 'left': 60}}, 'gdpPercap': {'filterTerm': '9', 'column': {'rowType': 'filter', 'key': 'gdpPercap', 'name': 'gdpPercap', 'editable': True, 'sortable': True, 'resizable': True, 'filterable': True, 'width': 147, 'left': 354}}, 'lifeExp': {'filterTerm': '4', 'column': {'rowType': 'filter', 'key': 'lifeExp', 'name': 'lifeExp', 'editable': True, 'sortable': True, 'resizable': True, 'filterable': True, 'width': 147, 'left': 501}}, 'pop': {'filterTerm': '3', 'column': {'rowType': 'filter', 'key': 'pop', 'name': 'pop', 'editable': True, 'sortable': True, 'resizable': True, 'filterable': True, 'width': 147, 'left': 648}}, 'year': {'filterTerm': '2007', 'column': {'rowType': 'filter', 'key': 'year', 'name': 'year', 'editable': True, 'sortable': True, 'resizable': True, 'filterable': True, 'width': 147, 'left': 795}}, 'country': {'filterTerm': 'af', 'column': {'rowType': 'filter', 'key': 'country', 'name': 'country', 'editable': True, 'sortable': True, 'resizable': True, 'filterable': True, 'width': 147, 'left': 207}}}
filters = {i:filters_raw[i]['filterTerm'] for i in filters_raw.keys()}

To use a dict to get exact matches I can do this Based on this answer( Filter a pandas dataframe using values from a dict ); 要使用字典来获得完全匹配,我可以基于此答案( 使用字典中的值过滤熊猫数据框)来做到这一点; ; ;

dff = df.loc[(df[list(filters)] == pd.Series(filters)).all(axis=1)]

But if I want to filter the same way, but not be limited to just exact matches but also get matches where value from dict is contained as a substring in dataframe. 但是,如果我想以相同的方式进行过滤,但不仅限于完全匹配,还可以获取匹配,其中dict中的值作为子字符串包含在数据帧中。 How would I do that? 我该怎么做?

The desired output is a dataframe with only the values that correspond to all the conditions simultaneously. 所需的输出是仅具有同时与所有条件对应的值的数据帧。 With the filters above; 使用上面的过滤器;

Dff
Asia Afghanistan 974.5803384 43.828 31889923 2007

Have a look at pandas.Series.str.contains where you can use a regular expression. 查看pandas.Series.str.contains ,您可以在其中使用正则表达式。 There is also string handling functions that may be more tailored for what you need. 还有一些字符串处理功能可能会针对您的需求进行量身定制。

One solution can be using pd.Series.str.starstwith to find strings matching the ones in filters . 一种解决方案是使用pd.Series.str.starstwith来查找与filters字符串匹配的字符串。

You can create a mask for those rows this way: 您可以通过以下方式为这些行创建掩码:

mask =  df.astype(str).apply(lambda x: x.str.lower()
        ).apply(lambda x: x.str.startswith(filters[x.name].lower()),
                axis=0).all(axis=1)

Basically, you convert the original dataframe to string and lower case and then go column by column checking wich elements start with the string in filter for that column (ie filters['continent'] ). 基本上,您将原始数据帧转换为字符串和小写字母,然后逐列检查其中的元素以该列的过滤器中的字符串开头(例如, filters['continent'] )。 Finally you set to true rows where all the cells contain the elements in filter 最后,将所有单元格都包含在filter的元素设置为真行

The result will be: 结果将是:

df[mask]

        country  year         pop continent  lifeExp   gdpPercap
11  Afghanistan  2007  31889923.0      Asia   43.828  974.580338

Hope it serves. 希望它有用。

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

相关问题 使用字典中的值过滤熊猫数据框的列和行 - Filter a pandas dataframe columns and rows using values from a dict 使用字典中的值过滤 pandas dataframe - Filter a pandas dataframe using values from a dict 根据部分字符串匹配,使用 pandas 在 python 中过滤 dataframe - Filter dataframe in python using pandas based on partial string match Pandas 数据框在多个列和值上进行 dict 列出 - Pandas dataframe to dict on multiple columns and values to list Python:熊猫列中的部分字符串匹配并从熊猫数据框中的其他列中检索值 - Python: Partial String matching in pandas column and retrieve the values from other columns in pandas dataframe 使用元组列表中的元组按多列过滤 pandas dataframe - Filter pandas dataframe by multiple columns, using tuple from list of tuples 从系列/ dict中的匹配列更新pandas数据帧行值 - Update pandas dataframe row values from matching columns in a series/dict 如何从 pandas dataframe 中的文本中匹配部分字符串 - how to match partial string from a text in pandas dataframe 使用 Pandas,如何从一个系列中的多个索引匹配,匹配到 DataFrame 并替换多个列 - Using Pandas, how can you match from multiple indexes in a series, match to a DataFrame and replace multiple columns 如何通过部分标签过滤pandas数据帧列 - How to filter pandas dataframe columns by partial label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM