程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Check a basic topic of pandas data processing (learning colored eggs at the end of the article)

編輯:Python

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 .

One 、 Preface

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 :

Two 、 Implementation process

Method 1

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 .

Method 2

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 ……

3、 ... and 、 summary

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?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved