0% found this document useful (0 votes)
15 views4 pages

Cheat Sheet Pandas

This document is a comprehensive cheat sheet for the Pandas library, covering various functionalities such as importing modules, loading and saving CSV files, converting datatypes, selecting and reshaping data, and performing operations on DataFrames. It includes code snippets and explanations for tasks like selecting rows and columns, adding new columns, renaming columns, and merging DataFrames. The cheat sheet serves as a quick reference for users to efficiently utilize Pandas for data manipulation and analysis.

Uploaded by

brhanegebregn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Cheat Sheet Pandas

This document is a comprehensive cheat sheet for the Pandas library, covering various functionalities such as importing modules, loading and saving CSV files, converting datatypes, selecting and reshaping data, and performing operations on DataFrames. It includes code snippets and explanations for tasks like selecting rows and columns, adding new columns, renaming columns, and merging DataFrames. The cheat sheet serves as a quick reference for users to efficiently utilize Pandas for data manipulation and analysis.

Uploaded by

brhanegebregn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Pandas Cheat Sheet

by Justin1209 (Justin1209) via cheatography.com/101982/cs/21202/

Import the Pandas Module Loading and Saving CSVs (cont) Converting Datatypes

import pandas as pd > # Get the first DataFrame chunk: # Convert argument to numeric type
df_urb_pop pandas.to​_nu​mer​ic(arg, errors​="ra​‐
Create a DataFrame df_urb_pop = next(u​rb_​pop​_re​ader) ise​")

# Method 1 errors:
Inspect a DataFrame "​rai​se" -> raise an exception
df1 = pd.Dat​aFr​ame({
df.head(5) First 5 rows "​coe​rce​" -> invalid parsing will be set as NaN
​ ​ 'name':
​ ['John Smith',
'Jane Doe'], df.info() Statistics of columns (row
DataFrame for Select Columns / Rows
​ ​ 'address':
​ ['13 Main St.', count, null values, datatype)
'46 Maple Ave.'], df = pd.DataFrame([

​ ​ 'age':
​ [34, 28] Reshape (for Scikit) ​ ​['J​anu​ary', 100, 100, 23,

}) 100],
nums = np.array(range(1, 11))
# Method 2 ​ ​['F​ebr​uary', 51, 45, 145,
-> [ 1 2 3 4 5 6 7 8 9 10]
df2 = pd.Dat​aFr​ame([ 45],
nums = nums.r​esh​ape(-1, 1)
​ ​ ​ ​['John Smith', '123 Main ​ ​['M​arch', 81, 96, 65, 96],
-> [ [1],
St.', 34], ​ ​['A​pril', 80, 80, 54, 180],
[2],
​ ​ ​ ​['Jane Doe', '456 Maple ​ ​['May', 51, 54, 54, 154],
[3],
Ave.', 28], ​ ​['J​une', 112, 109, 79,
[4],
​ ​ ​ ​['Joe Schmo', '9 129]],
[5],
Broadway', 51] ​ ​col​umn​s=[​'mo​nth',
[6],
​ ​ ​ ], 'east', 'north', 'south',
[7],
​ ​ columns=['name',
​ 'address', 'west']
[8],
'age']) )
[9],
[10]]
Loading and Saving CSVs Select Columns
You can think of reshape() as rotating this
# Load a CSV File in to a # Select one Column
array. Rather than one big row of numbers,
clinic​_north = df.north
DataFrame nums is now a big column of numbers -
df = pd.rea​d_c​sv(​'my​-cs​v- there’s one number in each row. --> Reshape values for Scikit

f​ile.csv') learn: clinic​_north.values.re​‐

# Saving DataFrame to a CSV File sha​pe(-1, 1)

df.to_​csv​('n​ew-​csv​-fi​‐ # Select multiple Columns

le.c​sv') clinic​_no​rth​_south
= df[['n​‐

# Load DataFrame in Chunks (For orth', 'south']]

large Datasets) Make sure that you have a double set of


# Initialize reader object: brackets [[ ]], or this command won’t work!
urb_po​p_r​eader
urb_po​p_r​eader = pd.rea​d_c​‐
sv(​'in​d_p​op_​dat​a.csv',
chunks​ize​=1000)

By Justin1209 (Justin1209) Published 23rd November, 2019. Sponsored by Readable.com


cheatography.com/justin1209/ Last updated 31st January, 2020. Measure your website readability!
Page 1 of 4. https://readable.com
Pandas Cheat Sheet
by Justin1209 (Justin1209) via cheatography.com/101982/cs/21202/

Select Rows Adding a Column Performing Column Operation (cont)

# Select one Row df = pd.DataFrame([ > -> lower, upper


march = df.iloc[2] ​ [1, '3 inch screw', 0.5, # Perform a lambda Operation on a Column
# Select multiple Rows 0.75], get_la​st_name = lambda x: x.spli​t(" "​)[-1]
jan_fe​b_march = df.ilo​c[:3] ​ [2, '2 inch nail', 0.10, df['la​st_​name'] = df.Name.apply(​get​_la​st_​‐
feb_ma​rch​_april = df.ilo​‐ 0.25], name)

c[1:4] ​ [3, 'hammer', 3.00, 5.50],


Performing a Operation on Multiple
may_june = df.ilo​c[-2:] ​ [4, 'screw​dri​ver', 2.50,
Columns
# Select Rows with Logic 3.00]
january = df[df.m​onth == ], df = pd.DataFrame([
'January'] ​ ​col​umn​s=[​'Pr​oduct ID', ​ ​["Ap​ple​", 1.00, "​No"],
-> <, >, <=, >=, !=, == 'Descr​ipt​ion', 'Cost to ​ ​["Mi​lk", 4.20, "​No"],
march_​april = df[(df.month == Manufa​cture', 'Price'] ​ ​["Paper Towels​", 5.00, "​‐
'March') | (df.month == ) Yes​"],
'April')] # Add a Column with specified ​ ​["Light Bulbs", 3.75, "​‐
-> &, | row-values Yes​"],
januar​y_f​ebr​uar​y_march = df['Sold in Bulk?'] = ['Yes', ],
df[df.month.isin(['Jan​uary', 'Yes', 'No', 'No'] ​ ​col​umn​s=[​"​Ite​m", "​‐
'Febru​ary', 'March'])] # Add a Column with same value Pri​ce", "Is taxed?​"])
-> column​_na​me.i​si​n([​" ", " in every row # Lambda Function
"]) df['Is taxed?'] = 'Yes' df['Price with Tax'] = df.app​‐
# Add a Column with calcul​ation ly(​lambda row:
Selecting a Subset of a Dataframe often
df['Re​venue'] = df['Pr​ice'] - ​ ​ ​ ​ ​row​['P​rice'] * 1.075
results in non-co​nse​cutive indices.
df['Cost to Manufa​cture'] ​ ​ ​ ​ if row['Is taxed?'] ==

Using .reset​_in​dex() will create a new 'Yes'

DataFrame move the old indices into a new Performing Column Operation ​ ​ ​ ​ else row['P​rice'],
colum called index. df = pd.DataFrame([
​ ​ ​axis=1

)
​ ​['JOHN SMITH', 'john.s​mi​‐
Use .reset​_in​dex​(dr​op=​True) if you dont th@​gma​il.c​om'], We apply a lambda to rows, as opposed to
need the index column. columns, when we want to perform functi​‐
​ ​['Jane Doe', 'jdoe@​yah​‐
Use .reset​_in​dex​(in​pla​ce=​True) to prevent a onality that needs to access more than one
oo.c​om'],
new DataFrame from brein created. column at a time.
​ ​['joe schmo', 'joesc​hmo​‐
@ho​tma​il.c​om']
],
column​s=[​'Name', 'Email'])
# Changing a column with an
Operation
df['Name'] = df.Name. apply(​‐
lower)

By Justin1209 (Justin1209) Published 23rd November, 2019. Sponsored by Readable.com


cheatography.com/justin1209/ Last updated 31st January, 2020. Measure your website readability!
Page 2 of 4. https://readable.com
Pandas Cheat Sheet
by Justin1209 (Justin1209) via cheatography.com/101982/cs/21202/

Rename Columns Column Statistics Pivot Tables

# Method 1 Mean = Average df.column.mean() orders =


df.columns = ['NewN​ame_1', Median df.column.median() pd.read_csv('orders.csv')
'NewNa​me_2, 'NewNa​me_3', shoe_c​ounts = orders.
Minimal Value df.column.min()
'...'] groupb​y([​'sh​oe_​type',
Maximum Value df.column.max()
# Method 2 'shoe_​col​or']).
Number of Values df.column.count()
df.rename(columns={ id.cou​nt(​).r​ese​t_i​ndex()
​ ​ ​ ​'Ol​dNa​me_1': 'NewNa​‐ Unique Values df.column.nunique() shoe_c​oun​ts_​pivot = shoe_c​‐
me_1', Standard Deviation df.column.std() oun​ts.p​ivot(
​ ​ ​ ​'Ol​dNa​me_2': 'NewNa​‐ List of Unique Values df.column.unique() index = 'shoe_​type',
me_2' columns = 'shoe_​color',
Dont't forget reset_​index() at the end of a
}, inplac​e=True
) values = 'id').r​es​et_​index()
groupby operation
Using inplac​e=True lets us edit the original We have to build a temporary table where
DataFrame. Calcul​ating Aggregate Functions we group by the columns we want to

# Group By include in the pivot table


Series vs. Dataframes
grouped = df. groupb​y([​'col1',
Merge (Same Column Name)
# Dataframe and Series 'col2']).col3
print(​typ​e(c​lin​ic_​nor​th)): .measur​ement()
. reset_​index() sales = pd.read_csv('sales.csv')
# <class 'panda​s.c​ore.se​rie​s.S​eri​es'> # -> group by column1 and targets = pd.rea​d_c​sv(​'ta​‐
print(​typ​e(df)): column2, calculate values of rge​ts.c​sv')
# <class 'panda​s.c​ore.fr​ame.Da​taF​ram​e'> column3 men_women = pd.rea​d_c​sv(​'me​‐
print(​typ​e(c​lin​ic_​nor​th_​south)) n_w​ome​n_s​ale​s.csv')
# Percentile
# <class 'panda​s.c​ore.fr​ame.Da​taF​ram​e'> # Method 1
high_e​arners = df.gro​upb​y('​‐
In Pandas cat​ego​ry'​).wage sales_​targets = pd.merge(sales,
- a series is a one-di​men​sional object that apply(​lambda
​ ​ ​ . x: np.per​‐ targets, how=" ")
contains any type of data. cen​tile(x, 75)) # how: "​inn​er"(​def​ault), "​‐
reset_​index()
​ ​ ​ . out​er", "​lef​t", "​rig​ht"
- a dataframe is a two-di​men​sional object #Method 2 (Method Chaining)
# np.per​centile can calculate
that can hold multiple columns of different all_data =
any percentile over an array of
types of data.
values sales.merge(targets).merge(men_w
omen)
A single column of a dataframe is a series, Don't forget reset.i​ndex()
and a dataframe is a container of two or
more series objects.

By Justin1209 (Justin1209) Published 23rd November, 2019. Sponsored by Readable.com


cheatography.com/justin1209/ Last updated 31st January, 2020. Measure your website readability!
Page 3 of 4. https://readable.com
Pandas Cheat Sheet
by Justin1209 (Justin1209) via cheatography.com/101982/cs/21202/

Inner Merge (Different Column Name) Melt

orders = pandas.me​lt(​Dat​aFrame, id_vars, value_​vars, var_name, value_​nam​e='​‐


pd.read_csv('orders.csv') value')
products = pd.rea​d_c​sv(​'pr​‐ id_vars: Column(s) to use as identifier variables.
odu​cts.csv') value_​vars: Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.
# Method 1: Rename Columns var_name: Name to use for the ‘variable’ column.
orders​_pr​oducts = value_​name: Name to use for the ‘value’ column.
pd.merge(orders, Unpivot a DataFrame from wide to long
products.rename(colum​ns=​{'i​‐ format, optionally leaving identi​fiers set.
d':​'pr​odu​ct_​id'}), how=" ")
.reset​_in​dex() Assert Statements
# how: "​inn​er"(​def​ault), "​‐ # Test if country is of type
out​er", "​lef​t", "​rig​ht" object
# Method 2: assert gapmin​der.co​unt​ry.d​‐
orders​_pr​oducts = types == np.object
pd.merge(orders, products, # Test if year is of type int64
​ ​ ​ ​ ​ ​ ​ ​ left_on="pr​‐
​ ​ ​ ​ ​ ​ ​ assert gapmin​der.ye​ar.d​types
odu​ct_​id", == np.int64
​ ​ ​ ​ ​ ​ ​ ​ right_on="id​",
​ ​ ​ ​ ​ ​ ​ # Test if life_e​xpe​ctancy is
​ ​ ​ ​ ​ ​ ​ ​ suffixes=["_​‐
​ ​ ​ ​ ​ ​ ​ of type float64
ord​ers​"​,"_p​rod​uct​s"]) assert gapmin​der.li​fe_​exp​‐
Method 2: ect​anc​y.d​types == np.float64
If we use this syntax, we’ll end up with two # Assert that country does not
columns called id. contain any missing values
Pandas won’t let you have two columns assert pd.not​nul​l(g​apm​ind​‐
with the same name, so it will change them er.c​ou​ntr​y).a​ll()
to id_x and id_y. # Assert that year does not
We can help make them more useful by contain any missing values
using the keyword suffixes. assert pd.not​nul​l(g​apm​ind​‐
er.y​ea​r).a​ll()
Concat​enate

bakery =
pd.read_csv('bakery.csv')
ice_cream = pd.rea​d_c​sv(​'ic​‐
e_c​rea​m.csv')
menu = pd.concat([bakery,
ice_cr​eam])

By Justin1209 (Justin1209) Published 23rd November, 2019. Sponsored by Readable.com


cheatography.com/justin1209/ Last updated 31st January, 2020. Measure your website readability!
Page 4 of 4. https://readable.com

You might also like