3

我有一个这样获得的结果数据框(参考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时,它已将frozenset转换为我不想要的字符串

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

任何帮助是极大的赞赏。谢谢

4

2 回答 2

2

一个经典的解决方案:

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
于 2019-03-07T17:28:58.970 回答
1

啊!发现了问题。str 访问器不起作用,因为该项目是对象,如错误所示,所以我必须首先使用 astype(str) 将其类型转换为 str,然后它才能工作

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

这会按我的预期过滤掉项目。

于 2019-03-07T19:18:59.967 回答