Show Menu

Pandas Cheat Sheet Rev2 Cheat Sheet by

Pandas Cheat Sheet Extra text to meet the character requirements

Import Data

df = pd.rea­d_c­sv­­('f­­il­e­n­am­­e.csv')
Read CSV into a Pandas DataFrame
df = pd.to_­csv­('f­­il­e­n­am­­e.csv')
Export Pandas DataFrame to CSV
Import Options:

header­=False, Index=­False, usecol­s=(5,6)

Can also read CSV / HTML / Excel / JSON

Combine multiple files into one (1) DataFrame

all_files = glob.glob(*/.txt')
Finds all txt files in the
df_raw = [­ad_­csv(f) for f in all_files]
Makes multiple DataFrames
df_all = pd.con­cat­(df­_raw, ignore­_in­dex­=True
Concat­enates all the DataFrames into one (1) large DataFrame

Select Data

Reads the first 5 rows
Reads the last 5 rows
Gives the number of columns and rows in the DataFrame

Select Row

First row of DataFrame
Second row of DataFrame

Select Column

First column of DataFrame
Second column of DataFrame

Select Column and Row Combined

df.ilo­c[:3, :2]
Selecting first 3 rows and first 2 columns
df.ilo­c[:3, ['Colu­mn1', 'Colum­n2']]
Selecting first 3 rows and first 2 columns

Re-Order Colums

df = df[['C­olu­mn3', 'Column2', 'Colum­n1']]
Re-orders the columns to the order specified in this list

Drop Columns

df= df.dro­p(c­olu­mns­=['­Col­umn1'], axis=1)
Drops 'Colum­n1_­Name' from DataFrame

Sort Columns

df = df.sor­t_v­alu­es(­by=­['C­olu­mn1'], ascend­ing­=False)
Sort values by Column1

Filter Column

df= df[(df­­['­C­o­lu­­mn1'] >= some_n­­umber]
Filter DataFrame by certain value

Rename Columns

df.columns = ['A', 'B']
Renamed Columns to 'A' and 'B'

Merge DataFrames

Joins df1 and df2

Filter on Condition

df = df[(df> 2).all­(ax­is=1)]
Removes any values less than 2

Select Row Based on Condition

row = df[df.A > 3].iloc[0]
Select first row where A > 0

Dealing with NAN values

df= df.fil­lna­(me­tho­d='­ffill')
Fills blank values using forward fill method
df= df.fil­lna­(me­tho­d='­bfill')
Fills blank values using backwards fill method
Removes rows with no values

Padding Values With Zero's

df['Co­lumn1'] = df['Co­lum­n1'­].a­sty­pe(­str­).s­tr.z­fi­ll(6)
Sets the numbe to six (6) long, which adds zeros

Change Column Data Type

df['Co­lum­n1']= df['Co­lum­n1'­].a­sty­pe(­float)
Change 'Column1' to float

Convert Column to Date / Time

df['Time'] = df['Ti­me'­].a­ppl­y(p­d.t­o_d­ate­time)
Converts the time column to a Datetime Series


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets