Click on the above “Python Home of sharing ”, Focus on
reply “ resources ” You can get a gift Python Learning materials
today
Japan
chicken
soup
Garrison drums and cut off people's travel , The sound of wild geese in the autumn .
Hello everyone , I'm Pippi .
A few days ago Python The strongest King exchange group 【Chloe】 Asked a question Pandas
Issues dealt with , As shown in the figure below .
The raw data are as follows :
df = pd.DataFrame( {'id' : ['A','A','A','A','A','A','B','B','B','B','B'],
'type' : [1,1,1,1,2,2,1,1,1,2,2],
'book' : ['Math','Math','English','Physics','Math','English','Physics','English','Physics','English','English']})
res = df.groupby(['id','type']).book.apply(list).reset_index()
res['book'] = res.apply(lambda x:(','.join([str(i) for i in x['book']])))
res
The expected results are shown in the following figure :
here 【 Luna 】 Give a feasible code , We met later , You can modify it accordingly , Yield twice the result with half the effort , The code is as follows :
df.groupby(['id','type']).book.unique().str.join(',').reset_index()
After running , The result is what you want .
later 【 Mr. Yu Liang 】 Also gave a code .
The code is as follows :
res = df.groupby(['id', 'type']).book.apply(set).reset_index()
res['book'] = res['book'].apply(lambda x: (','.join(x)))
print(res)
After running , The result is what you want .
Perfectly solved the problem of fans !
Finally, I would like to share a knowledge point with you , As shown in the figure below .
later 【 Mr. Yu Liang 】 It also adds a conclusion , Here to share with you .
The article makes a comparison :
Ordinary apply Processing needs 18 About seconds ,
Use Swift Accelerate to 7 About seconds ,
Function vectorization , Time reduced to 0.4 second ,
int64 To int16, Time reduced to 0.1 second
Convert to as much as possible .values, Then operate , 2-seeded 0.07 second !!!
from 18 Seconds to 0.07 second ……
Hello everyone , I'm Pippi . This article mainly reviews the use of Pandas
Dealing with data , In this paper, the specific analysis and code implementation are given , There are two ways , Help the fans solve the problem smoothly .
Finally, thank the fans 【Chloe】 put questions to , thank 【 Luna 】、【 Mr. Yu Liang 】 Given the idea and code analysis , thank 【dcpeng】、【 Feng Cheng 】、【 Cold meow 】、【D I Y】 And others participate in learning and communication .
friends , Practice it quickly ! If in the process of learning , Any problems encountered , Welcome to add my friend , I'll pull you in Python The learning exchange group discusses learning together .
------------------- End -------------------
Excellent articles in the past are recommended :
An article will take you through Pandas mapping API
Netease cloud music popular works name and link capture (html5lib piece )
Netease cloud music popular works name and link capture (pyquery piece )
Netease cloud music popular works name and link capture (bs4 piece )
Welcome to give the thumbs-up , Leaving a message. , forward , Reprint , Thank you for your company and support
Want to join Python Learning group, please reply in the background 【 Enter into Group 】
All rivers and mountains are always in love , Order one 【 Looking at 】 OK?