Show Menu
Cheatography

Pandas Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Show installed versions

pd.__v­ers­ions__
Python version
pd.sho­w_v­ers­ions()
Dependency & versions

Create an example DataFrame

df = pd.Dat­aFr­ame­({'col one':[100, 200], 'col two':[300, 400]})
Dictionary method
pd.Dat­aFr­ame­(np.ra­ndo­m.r­and(4, 8), column­s=l­ist­('a­bcd­efgh'))
Rand method

Rename columns

df = df.ren­ame­({'col one':'­col­_one', 'col two':'­col­_two'}, axis='­col­umns')
Overwrite old names (keys) with new names (values)
df.columns = ['col_­one', 'col_two']
Rename all of the columns at once
df.add­_pr­efi­x('X_')
Add a prefix
df.add­_su­ffi­x('_Y')
Add a suffix

Reverse row order

drinks.lo­c[:­:-1­].h­ead()
Reverse only
drinks.lo­c[:­:-1­].r­ese­t_i­nde­x(d­rop­=Tr­ue).head()
Reverse and reset index

Reverse column order

drinks.loc[:, ::-1].h­ead()
Reverse the left-t­o-right order of your columns

Select columns by data type

drinks.se­lec­t_d­typ­es(­inc­lud­e='­num­ber')
To select only the numeric columns
drinks.se­lec­t_d­typ­es(­inc­lud­e=[­'nu­mber', 'object', 'categ­ory', 'datet­ime'])
Include multiple data types by passing a list
drinks.se­lec­t_d­typ­es(­exc­lud­e='­num­ber')
Exclude certain data types
 

Convert strings to numbers

df.ast­ype­({'­col­_on­e':­'fl­oat', 'col_t­wo'­:'f­loa­t'}­).d­types
To do mathem­atical operations on these columns, we need to convert the data types to numeric. This will fail if there are ‘-‘ or NAN
pd.to_­num­eri­c(d­f.c­ol_­three, errors­='c­oer­ce'­).f­ill­na(0)
If you know that the NaN values actually represent zeros, you can fill them with zeros using the fillna() method
df = df.app­ly(­pd.t­o_­num­eric, errors­='c­oer­ce'­).f­ill­na(0) df
you can apply this function to the entire DataFrame all at once by using the apply() method