У меня есть результирующий фрейм данных, который я получил вот так (ref 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. Я хочу отфильтровать результаты, которые показывают все строки, в которых наборы элементов содержат выбранную мной строку, например, я хочу показать строки, содержащие «яйца», и результат будет
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, он превращает заморозку в строку, которую я не хочу
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
Любая помощь очень ценится. Спасибо