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

Tips | share several pandas efficient functions

編輯:Python

Hello everyone ~

In this issue, I recommend some pandas Efficient data processing functions ( Continuous updating ), I hope it helped you :

  1. Dictionary creation Dataframe
  2. Column splitting (split/extract)
  3. columns (cat)
  4. Fill left and right (pad)
  5. Filter columns by type (select_dtypes)
  6. Sort (rank)

1. Dictionary creation Dataframe

df_dict = {'name':['Alice_001','Bob_002','Cindy_003','Eric_004','Helen_005','Grace_006'],'sex':['female','male','female','male','female','male'],'math':[90,89,99,78,97,93],'english':[95,94,80,94,94,90]}
#[1]. Write parameters directly test_dict
df = pd.DataFrame(df_dict)
#[2]. Dictionary assignment
df = pd.DataFrame(data=df_dict)

2. Column splitting (split/extract)

Character splitting :

df1[['name', 'id']] = df1['name'].str.split('_', 2, expand = True)

Regular expression splitting :

df2 = df.copy()
df2['name2'] = df2['name'].str.extract('([A-Z]+[a-z]+)')
df2['id2'] = df2['name'].str.extract('(\d+)')

3. columns (cat)

Custom connector :

df1["name_id"] = df1["name"].str.cat(df1["id"],sep='_'*3)

Merge output of a column :

df1["name"].str.cat(sep='*'*5)

4. Fill left and right (pad)

padding-left :

df1["id"] = df1["id"].str.pad(10,fillchar="*")
# amount to ljust()
df1["id"] = df1["id"].str.rjust(10,fillchar="*")

Right fill :

df1["id"] = df1["id"].str.pad(10,side="right",fillchar="*")

Fill both sides :

df1["id"] = df1["id"].str.pad(10,side="both",fillchar="*")

5. Filter columns by type (select_dtypes)

Filter numeric Columns :

df1.select_dtypes(include=['float64', 'int64'])

Screening object Column :

df1.select_dtypes(include=['object'])

6. Sort (rank)

English achievement ranking :

df1['e_rank'] = df1['english'].rank(method='min',ascending=False)

94 There are three , So the three are tied for the first place 2.

The above is all the contents sorted out for you in this issue , Practice quickly


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