Home>

In the following data frame

import pandas as pd
item = [frozenset (['a']), frozenset (['b']), frozenset (['c']), frozenset (['d']), frozenset (['a', 'b'] ), frozenset (['a', 'b', 'c'])]
df = pd.DataFrame ({'N': item})
N
0 (a)
1 (b)
2 (c)
3 (d)
4 (a, b)
5 (a, b, c)

I have extracted one item as follows. Is there any other good way?

items = set ()
for index, row in df.iterrows ():
    for i in row ['N']:
        items.add (i)
df_items = pd.DataFrame (columns = ['N'])
for item in items:
    df_items = df_items.append (df [df ['N'] == frozenset (item)])
df_items = df_items.reset_index (drop = True)
df_items
N
0 (a)
1 (b)
2 (c)
3 (d)
  • Answer # 1

    df [df ['N']. map (len) == 1]

Related articles