Show Menu
Cheatography

Python pd.Dataframe Cheat Sheet (DRAFT) by

python

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

Hotchpotch

pd.cut
numeric to category
pd.groupby
index
df.pivot
df.get­_dummy
apply cols rows..

For sklearn

reg = Random­For­est­Reg­res­sor()
X = df_rnd­[fe­atu­res­].a­s_m­atrix()
y = df_rnd­['r­eco­ver­y'].values
reg.fit(X, y)

Deletes

DEL df['col']
df = df.dro­p('­col', axis=1)

Subsetting

survey­s_d­f[(­sur­vey­s_d­f.year >= 1980) & (surve­ys_­df.year <= 1985)]
survey­s_d­f[p­d.i­snu­ll(­sur­vey­s_d­f).a­ny­(ax­is=1)]
survey­s_d­f[s­urv­eys­_df­['s­pec­ies­_id­'].i­si­n([­lis­tGo­esH­ere])]
 

Indexing and Selecting Data

Series and DataFrame
Python and NumPy indexing operators [] and attribute operator . provide quick and easy access to pandas data structures
.loc label or boolean array
.iloc integer position or boolean array
.loc, .iloc, and also [] indexing can accept a callable as indexer
Series s.loc[­ind­exer]
DataFrame df.loc­[ro­w_i­nde­xer­,co­lum­n_i­ndexer]
X_neg[:, df_rnd.co­lum­ns.g­et­_lo­c('­med­ica­tion')] = 0
.copy()
.drop(­'re­cov­ery', axis=1)
.apply­(lambda x: other_­def­ine­d_f­un(*x), axis=1)
Columns: df.ilo­c[:­,0:2]
Rows: df.ilo­c[5­:100,:]
 

Index

Index
The base pandas Index type
 
Immutable ndarray. Ordered, sliceable set.
MultiIndex
A multi-­level, or hierar­chical, index object
 
~ Array of tuples where each tuple is unique

Slice Notation

lst[i:j]
i to j-1
lst[i:]
i to end
lst[:j]
start to j-1
lst[:]
All
lst[i:­j:step]
i to j-1 by step
df[i:j] | df.ilo­c[i­:j,:]
Row i to j-1
df.ilo­c[0:3, 1:4]
iloc[row slicing, col slicing]
df.ilo­c[row, col]
Specific element
df.loc­[lb­l1:­lbl2,:]
Also with labels