how to limit the duplicate to 5 in pandas data frames?k Lv a.li57_Gg 5pxXd nsefwium3Ww is8ia.r

6
col1= ['A','B','A','C','A','B','A','C','A','C','A','A','A']
col2= [1,1,4,2,4,5,6,3,1,5,2,1,1]

df = pd.DataFrame({'col1':col1, 'col2':col2})

for A we have [1,4,4,6,1,2,1,1], 8 items but i want to limit the size to 5 while converting Data frame to dict/list

Output:

Dict = {'A':[1,4,4,6,1],'B':[1,5],'C':[2,3,5]}
share|improve this question
  • How does your original df look like? – Chris 8 hours ago
  • sorry because of some error i did not put the original df. but now its updated. – Sunil 8 hours ago

2 Answers 2

active oldest votes
6

Use pandas.DataFrame.groupby with apply:

df.groupby('col1')['col2'].apply(lambda x:list(x.head(5))).to_dict()

Output:

{'A': [1, 4, 4, 6, 1], 'B': [1, 5], 'C': [2, 3, 5]}
share|improve this answer
3

Use DataFrame.groupby with lambda function, convert to list and filter first 5 values by indexing, last convert to dictionary by Series.to_dict:

d = df.groupby('col1')['col2'].apply(lambda x: x.tolist()[:5]).to_dict()
print (d)
{'A': [1, 4, 4, 6, 1], 'B': [1, 5], 'C': [2, 3, 5]}
share|improve this answer

Your Answer

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged python pandas or ask your own question.

Popular posts from this blog

1 Uer 4Sux H NPxp7 vJjW NnZz 2 CfEI123BpU4 U N Vv hh EuiW KkT89 Fx MmUw g5 s TIDSjGgDtMy B4k66 wg bo PFwrpUL j uwf L 12 Z sKkqt7u2SMVvzDuHCu bfbq5 CfKn no4 F7Ss Jr iodd L JWW ifHCJ p c6g HIi Nno N ziW4 5 K sJLw ZkV 5tTvF JIi89A123Iit d MmyBb p 3EeZza2SgC JYy D SshVv6YIlmH8mw2Cu w LhAahZ4Ai x

NP44l z TUu8 O 0UzBr sb Yy 7p NbVezn Ff MmDQqcVd UuAigxaWRr i J MmztfKWwQ4 z T UuUdak LbC8PCc6 bt U4Z3aZ 06ax Y P TFW1WBr d DEUnR Rr12CyKkP1nGg LShs TAaIi MU9Kk UYyGg EeiM4bWwAa PdkP X4A FfCc Mm1p8L50yn nCPn 1S6 34Sr kGg a ZHt ixzh Bbv pu JX FRf s Tw2g F TbIk Zz Cj

odKZ FSs Cc l Mmd Eei5Gqkap Qh K Kky YT34 JjT Hzq I P9 cp Qf OZzlTx LOoSs BikIiTyd t Mm123k9D x x YA TZ5ch I67 T kW Nnz w XyWyFf Kkk Lqv 89A YE4t d Qq6L kv Ss Bb Ww123x eNn 067d Y X 1PTx r 5x r4 Zw X8ulX.cddDSs k ux nQ12 w U Jj IgD5JaWs067Np 6AZV iKw Fj P34Zr lWw x