python将字典中所有键以列表形式输出,Python Pandas:如何以字典形式返回列中的分组列表...

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

解决方案

Edited using agg from @ayhan (much faster than apply).

from collections import Counter

df.groupby("id")["val"].agg(lambda x: Counter([a for b in x for a in b]))

Out:

id

a {'val2': 2, 'val6': 1, 'val7': 1, 'val1': 1}

b {'val9': 1, 'val33': 1, 'val6': 1}

Name: val, dtype: object

Time of this version:

%timeit df.groupby("id")["val"].agg(lambda x: Counter([a for b in x for a in b]))

1000 loops, best of 3: 820 µs per loop

Time of @ayhan version:

%timeit df.groupby('id')["val"].agg(lambda x: pd.Series([a for b in x.tolist() for a in b]).value_counts().to_dict() )

100 loops, best of 3: 1.91 ms per loo