获取列表的前2个值

时间:2018-03-07 11:09:45

标签: python pandas

在下面的数据框中,我有一个值列表,我们如何从列表中重复前2个?

数据帧:

user    pro
A   [AA,AA,AA,BB,CC,AA,AA,CC,CC,BB]
B   [AA, BB, EE,BB,BB,EE,AA,CC,BB,EE]
C   [EE,EE,EE,CC,CC,CC,CC,DD,DD,AA]
D   [DD,AA,AA,AA,AA,AA,BB,BB,BB]

预期产出:

A   [AA,CC]
B   [BB,EE]
C   [CC,EE]
D   [AA,BB]

2 个答案:

答案 0 :(得分:4)

使用collections.Counter.most_common

from collections import Counter

df['new']  = df['pro'].apply(lambda x: [k for k, v in Counter(x).most_common(2)])
print (df)
  user                                       pro       new
0    A  [AA, AA, AA, BB, CC, AA, AA, CC, CC, BB]  [AA, CC]
1    B  [AA, BB, EE, BB, BB, EE, AA, CC, BB, EE]  [BB, EE]
2    C  [EE, EE, EE, CC, CC, CC, CC, DD, DD, AA]  [CC, EE]
3    D      [DD, AA, AA, AA, AA, AA, BB, BB, BB]  [AA, BB]

谢谢@jpp:

df['common'] = [list(zip(*d.most_common(2)))[0] for d in df['pro'].map(Counter)]

谢谢@cᴏʟᴅsᴘᴇᴇᴅ:

df['common'] = df.pro.map(lambda x: [k for k, v in Counter(x).most_common(2)])

答案 1 :(得分:0)

我没有意识到collections.Counter.most_common我想出了:

from collections import Counter
df['result'] = df['pro'].apply(lambda x: [i[0] for i in sorted(Counter(x).items(), key=lambda x: -x[1])[:2]])