python - 对包含卡住字符串的 pandas 数据框列进行过滤

标签 python pandas dataframe frozenset

我有一个像这样获得的结果数据框(引用http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/)

dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
           ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
           ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],
           ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],
           ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder

te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
df
from mlxtend.frequent_patterns import apriori
file_result_df = apriori(df, min_support=0.6, use_colnames=True)
file_result_df['length'] = file_result_df['itemsets'].apply(lambda x: len(x))

file_result_df
  support itemsets    length
0 0.8 (Eggs)  1
1 1.0 (Kidney Beans)  1
2 0.6 (Milk)  1
3 0.6 (Onion) 1
4 0.6 (Yogurt)    1
5 0.8 (Eggs, Kidney Beans)    2
6 0.6 (Onion, Eggs)   2
7 0.6 (Milk, Kidney Beans)    2
8 0.6 (Onion, Kidney Beans)   2
9 0.6 (Kidney Beans, Yogurt)  2
10 0.6 (Onion, Eggs, Kidney Beans)    3

“itemsets”列包含 python freezeset 数据。我想过滤掉显示项目集包含我选择的字符串的所有行的结果,例如我想显示包含“eggs”的行,结果将是

  support itemsets    length
0 0.8 (Eggs)  1
5 0.8 (Eggs, Kidney Beans)    2
6 0.6 (Onion, Eggs)   2
10 0.6 (Onion, Eggs, Kidney Beans)    3

我已经按照这里的建议尝试了 http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/

这给了我空的 df

fname = 'eggs'
file_result_df = file_result_df[ file_result_df['itemsets'] == frozenset((fname)) ]

这只给我第一行,即

support itemsets    length
0.8 (Eggs)  1
file_result_df = file_result_df[ file_result_df['itemsets'] == {fname} ]

这给了我错误

fname = 'eggs'
file_result_df = file_result_df[file_result_df['itemsets'].str.lower().str.contains(fname)]

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-152-cb30c651c2b0> in <module>
      1 fname = 'eggs'
----> 2 result_df = result_df[result_df['itemsets'].str.lower().str.contains(fname)]

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5061         if (name in self._internal_names_set or name in self._metadata or
   5062                 name in self._accessors):
-> 5063             return object.__getattribute__(self, name)
   5064         else:
   5065             if self._info_axis._can_hold_identifiers_and_holds_name(name):

/opt/conda/lib/python3.6/site-packages/pandas/core/accessor.py in __get__(self, obj, cls)
    169             # we're accessing the attribute of the class, i.e., Dataset.geo
    170             return self._accessor
--> 171         accessor_obj = self._accessor(obj)
    172         # Replace the property with the accessor object. Inspired by:
    173         # http://www.pydanny.com/cached-property.html

/opt/conda/lib/python3.6/site-packages/pandas/core/strings.py in __init__(self, data)
   1794 
   1795     def __init__(self, data):
-> 1796         self._validate(data)
   1797         self._is_categorical = is_categorical_dtype(data)
   1798 

/opt/conda/lib/python3.6/site-packages/pandas/core/strings.py in _validate(data)
   1816             # (instead of test for object dtype), but that isn't practical for
   1817             # performance reasons until we have a str dtype (GH 9343)
-> 1818             raise AttributeError("Can only use .str accessor with string "
   1819                                  "values, which use np.object_ dtype in "
   1820                                  "pandas")

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

这似乎有效

file_result_df = file_result_df[file_result_df['itemsets'].astype(str).str.contains(fname)]

但是当我打印 df 时,它已将 freezeset 转换为我不想要的字符串

support itemsets length
0   0.8 frozenset({'Eggs'}) 1
5   0.8 frozenset({'Eggs', 'Kidney Beans'}) 2
6   0.6 frozenset({'Onion', 'Eggs'}) 2
10  0.6 frozenset({'Onion', 'Eggs', 'Kidney Beans'}) 3

非常感谢任何帮助。谢谢

最佳答案

一个经典的解决方案:

fname = 'eggs'
file_result_df = file_result_df[file_result_df['itemsets'].astype(str).str.lower().str.contains(fname)]

输出:

    support                     itemsets  length
0       0.8                       (Eggs)     1.0
5       0.8         (Eggs, Kidney Beans)     2.0
6       0.6                (Onion, Eggs)     2.0
10      0.6  (Onion, Eggs, Kidney Beans)     3.0

关于python - 对包含卡住字符串的 pandas 数据框列进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55049392/

相关文章:

python - 如何绘制堆叠和归一化直方图?

python - Pygame: Rect() 参数到底是什么?它们代表什么?

python - 从多个 Excel 文件创建 Pandas 数据框

python - 导入 BERT : module 'tensorflow._api.v2.train' has no attribute 'Optimizer' 时出错

python - 使用python从文本文件中获取行的交集

dataframe - 通过传递列表来连接数据框列

python - 如何计算列的平均值但只包括某些行?

python - Pandas 按组聚合和列排序

python - 从索引的列/行数组中填充出现的矩阵

python 模拟补丁装饰器对于类方法和单个函数的行为不同