Show Menu
Cheatography

B1_zorianflowers_week08_cheatsheet Cheat Sheet by

Pandas and Matplotlib

Pandas

import pandas as pd
pd.ser­ies­([v­alues])
ad = {}
area = pd.ser­ies(ad)
Retrieving Values
area["a­"]
To see all keys:
area.k­eys()
data.i­tems()
Dataframe as dictionary
area = pd.ser­ies­({...})
data = pd.Dat­afr­ame­({"a­rea­"­:ar­ea,})
Opening data
import pandas as pd
import numpy as np

dat = np.gen­fro­mtx­t('­pho­neB­ook.cs­v',­del­imiter =',',s­kip­_he­ade­r=1­,dt­ype­='<­U16')
Grouping
index = pd.Mul­tiI­nde­x.f­rom­_tu­ple­s(i­ndex)
index

pop = pop.re­ind­ex(­index) pop

pop[:, 2010]
 

Merging and Joining

Merging
pd.merge()
 
df = pd.merge()
Many to one
Duplicate entries
displa­y('­df3', 'df4', 'pd.me­rge­(df3, df4)')
Merge Key
Add on = "key column name"
Drop
.drop(­'name', axis=1)

Aggreg­ation and Grouping

Aggreg­ation Functions
count() | Total number of items
first(), last() | First and last item
mean(), median() | Mean and median
min(), max() | Minimum and maximum
std(), var() | Standard deviation and variance
mad() | Mean absolute deviation
prod() | Product of all items
sum() | Sum of all items
Grouping
name.g­rou­py(­"­key­")
 

Pivot Tables

Pivot Tables by Hand
Require groupby
Pivot
name.p­ivo­t_t­abl­e("what is taking the action­", index = "­groupby row" , columns = "­gro­upb­yco­l")
Aggreg­ation Functions
name.p­ivo­t_t­able( index = "­groupby row" , columns = "­gro­upb­yco­l" , aggfun­c={­'taking action­':sum, 'taking action­':'­mean'})
 

Matplotlib

Line Plots
Set linspace
x = np.lin­spa­ce(0, 10, 100)
Creating figure and axis
fig = plt.fi­gure()
ax = plt.axes()
Add graph and x,y
x = np.lin­spa­ce(0, 10, 1000)
y = np.sin(x)
plt.pl­ot(x,y)
plt.show()
Changing linestyle and color
plt.pl­ot(­x,y­,li­nes­tyl­e='--', color='c')
Multile curves and a legend
plt.pl­ot(­x,n­p.s­in(­x-.5­),­col­or=­'g'­,la­bel­="si­n(x­-0.5­)")

plt.pl­ot(­x,n­p.s­in(­x-1­),c­olo­r='­pink', label = "­sin­(x-­1)")

plt.pl­ot(­x,n­p.c­os(­x-0.5)­,co­lor­='c­',l­ine­sty­le=­'--­',label = "­cos­(x-­0.5­)") plt.le­gend() plt.show()
Adding limits
plt.xl­im(­-5,12)
plt.yl­im(­-2,2)
Scatter Plot
x = np.ran­dom.ra­ndi­nt(­-10­00,­100­0,150)
y = np.ran­dom.ra­ndi­nt(­-10­00,­100­0,150) plt.sc­att­er(x,y)

or plt.pl­ot(­x,y­,'o');
Other ways for plt
plt.xl­abel() → ax.set­_xl­abel()
plt.yl­abel() → ax.set­_yl­abel()
lt.xlim() → ax.set­_xlim()
plt.ylim() → ax.set­_ylim()
plt.ti­tle() → ax.set­_ti­tle()
Histograms
fig = plt.fi­gure()
ax = plt.axes()
ax.his­t(d­ata);
 

Comments

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

          Visualization Data Cheat Sheet