python 3.x - How to create multiple value dictionary from pandas data frame -
lets have pandas data frame 2 columns(column , column b): values in column 'a' there multiple values in column 'b'. want create dictionary multiple values each key values should unique well. please suggest me way this.
one way groupby columns a:
in [1]: df = pd.dataframe([[1, 2], [1, 4], [5, 6]], columns=['a', 'b']) in [2]: df out[2]: b 0 1 2 1 1 4 2 5 6 in [3]: g = df.groupby('a')
apply tolist
on each of group's column b:
in [4]: g['b'].tolist() # shorthand .apply(lambda s: s.tolist()) "automatic delegation" out[4]: 1 [2, 4] 5 [6] dtype: object
and call to_dict
on series:
in [5]: g['b'].tolist().to_dict() out[5]: {1: [2, 4], 5: [6]}
if want these unique, use unique
(note: create numpy array rather list):
in [11]: df = pd.dataframe([[1, 2], [1, 2], [5, 6]], columns=['a', 'b']) in [12]: g = df.groupby('a') in [13]: g['b'].unique() out[13]: 1 [2] 5 [6] dtype: object in [14]: g['b'].unique().to_dict() out[14]: {1: array([2]), 5: array([6])}
other alternatives use .apply(lambda s: set(s))
, .apply(lambda s: list(set(s)))
, .apply(lambda s: list(s.unique()))
...
Comments
Post a Comment