Python Pandas : How to compile all lists in a column into one unique list
Starting with data from previous question:
f = pd.DataFrame({'id':['a','b', 'a'], 'val':[['val1','val2'],
['val33','val9','val6'],
['val2','val6','val7']]})
print (df)
id val
0 a [val1, val2]
1 b [val33, val9, val6]
2 a [val2, val6, val7]
How do I get the lists into Dict:
pd.Series([a for b in df.val.tolist() for a in b]).value_counts().to_dict()
{'val1': 1, 'val2': 2, 'val33': 1, 'val6': 2, 'val7': 1, 'val9': 1}
How do I get the lists by groups:
df.groupby('id')["val"].apply(lambda x: (list([a for b in x.tolist() for a in b]))
)
id
a [val1, val2, val2, val6, val7]
b [val33, val9, val6]
Name: val, dtype: object
How do I get the lists by groups as dicts:
df.groupby('id')["val"].apply(lambda x: pd.Series([a for b in x.tolist() for a in b]).value_counts().to_dict() )
Returns:
id
a val1 1.0
val2 2.0
val6 1.0
val7 1.0
b val33 1.0
val6 1.0
val9 1.0
Name: val, dtype: float64
Desired output What am I overlooking? :
id
a {'val1': 1, 'val2': 2, 'val6': 2, 'val7': 1}
b {'val33': 1, 'val6': 1, 'val9': 1}
Name: val, dtype: object