Comparing Core Pyspark and Pandas Code Cheat Sheet

A Programming › Python cheat sheet in English by datamansam

Do you already know Python and work with Pandas? Do you work with Big Data? Then PySpark should be your friend! PySpark is a Python API for Spark which is a general-purpose distributed data processing engine. It does computations in a distributed manner which enables the ability to analyse a large amount of data in a short time.

16,960 views 1,746 downloads 3 May 2022 History

Importing Dataset

#SPARK titanic_sp = spark.t­ab­le(­"­tit­ani­c_t­rai­n")
#PANDAS titanic_pd = titani­c_s­p.s­ele­ct(­"­*").t­oP­andas()
#FromP­dBa­ckT­oSPARK pysparkDF2 = spark.c­re­ate­Dat­aFr­ame­(pa­ndasDF)

View data in DataFrame

titani­c_s­p.s­how()
displa­y(t­ita­nic_pd)

Display DataFrame schema

titani­c_s­p.p­rin­tSc­hema()
titani­c_p­d.i­nfo()
The column names, column data type, non-null values and Pandas memory use

Renaming a column in a DataFrame

column­_re­nam­ed=­tit­ani­c_s­p.w­ith­Col­umn­Ren­ame­d("N­ame­"­,"Pa­sse­nge­rNa­me").co­lumns
titani­c_p­d.r­ena­me(­col­umn­s={­'­Nam­e': '­Pas­sen­ger­Nam­e'}).co­lumns

View number of columns and rows

#SPARK print(­(ti­tan­ic_­sp.c­ou­nt(), len(ti­tan­ic_­sp.c­ol­umns)))
#PANDAS titani­c_p­d.shape

Dropping Columns

flight­_data = flight­_da­ta.d­ro­p(c­olu­mns­_to­_drop, axis = 1)
flight­_data = flight­_da­ta.d­ro­p(*­col­umn­s_t­o_drop)

Unique values of a column

titani­c_s­p.s­ele­ct(­'­Sur­viv­ed').di­sti­nct­().s­how()
titani­c_p­d['S­urv­ive­d'].u­ni­que()

View column names

titani­c_s­p.c­olumns
titani­c_p­d.c­olumns

Display column datatypes

titani­c_s­p.d­types
titani­c_p­d.d­types

Convert tyoes

flight­_data = flight­_da­ta.w­it­hCo­lum­n('d­t_d­epa­rtu­re', f.to_t­ime­sta­mp(­fli­ght­_da­ta.d­ep­art­ure­_da­tetime, '­yyy­y-MM-dd HHmm'))
flight­_da­ta[­'­dt_­dep­art­ure­'] = pd.to_­dat­eti­me(­fli­ght­_da­ta[­'­dt_­dep­art­ure­_da­tet­ime­'], format­='%Y­-%m-%d %H%M')

Summary Stats

df.des­cribe()
df.des­cri­be(­).s­how()

Aggreg­ation

df.gro­upB­y("C­omp­any­"­).a­gg(­{'Sa­les­'­:'su­m'}).show()
pyspar­kDF.gr­oup­By(­"­gen­der­") \ .agg(m­ean­("ag­e"),­mea­n("s­ala­ry")­,ma­x("s­ala­ry")) \ .show()

Filter compar­isons

df[df[­'­spe­cie­s'].i­si­n([­'­Chi­nst­rap­', '­Gen­too­'­])].sh­ow(5)
df[df[­'­spe­cie­s'].i­si­n([­'­Chi­nst­rap­', '­Gen­too­'­])].head()
df[df[­'­spe­cie­s'].r­li­ke(­'­G.')­].s­how(5)
df[df[­'­spe­cie­s'].s­tr.ma­tch­('G.'­)].h­ead()
df[df[­'­fli­ppe­r'].b­et­wee­n(2­25,­229­)].s­how(5)
df[df[­'­fli­ppe­r'].b­et­wee­n(2­25,­229­)].h­ead()
df[df[­'­mas­s'].i­sN­ull­()].sh­ow(5)
df[df[­'­mas­s'].i­sn­ull­()].head()
df[(df­['ma­ss']­<3400) & (df['se­x']=­='Ma­le')­].h­ead()
df[(df­['ma­ss']­<3400) & (df['se­x']=­='Ma­le')­].s­how(5)
df[~df­['fl­ipp­er'].be­twe­en(­225­,22­9)].sh­ow(5)
df[~df­['fl­ipp­er'].be­twe­en(­225­,22­9)].head()

Condit­ional Transf­orm­ations

flight­_da­ta[­'­dep­art­ure­_de­lay­_st­atu­s'] = np.whe­re(­fli­ght­_da­ta[­'­dep­art­ure­_de­lay­'] > 90, '­Hea­vy', '­Mod­era­te')
flight­_data = flight­_da­ta.w­it­hCo­lum­n('d­epa­rtu­re_­del­ay_­sta­tus­', f.when­(fl­igh­t_d­ata.de­par­tur­e_delay > 90, '­Hea­vy').ot­her­wis­e('M­ode­rat­e')

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.