Tips | share several pandas efficient functions

Manon rushes forward 2022-06-23 17:46:43 阅读数:207

tipssharepandasefficientfunctions

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

版权声明:本文为[Manon rushes forward]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/01/202201052150194902.html