我有一个这样获得的结果数据框(参考http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/)
1
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
'itemsets'列包含python Frozenset数据。我想过滤掉显示所有行的项目集,这些行的项目集包含我选择的字符串,例如,我想显示包含“蛋”的行,结果将是
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
我尝试过这里的建议http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/
这给我空的df
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
这仅给我第一行
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
但是当我打印df时,它已将Frozenset转换为我不想要的字符串
file_result_df = file_result_df[file_result_df['itemsets'].astype(str).str.contains(fname)]
任何帮助将不胜感激。谢谢
答案 0 :(得分:1)
经典解决方案:
sku = Object.assign({}, sku,{selected:false})
输出:
fname = 'eggs'
file_result_df = file_result_df[file_result_df['itemsets'].astype(str).str.lower().str.contains(fname)]
答案 1 :(得分:0)
啊!找出问题所在。 str访问器不起作用,因为该项目是错误显示的对象,因此我必须先使用astype(str)将其类型转换为str,然后才能工作
file_result_df = file_result_df[file_result_df['itemsets'].astype(str).str.contains(fname)]
这会按预期过滤掉项目。