\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Taissir Boukrouba (taissir2002)} \pdfinfo{ /Title (pandas-essentials.pdf) /Creator (Cheatography) /Author (Taissir Boukrouba (taissir2002)) /Subject (Pandas Essentials Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{87BCC7} \definecolor{LightBackground}{HTML}{F0F6F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Pandas Essentials Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Taissir Boukrouba (taissir2002)} via \textcolor{DarkBackground}{\uline{cheatography.com/193976/cs/41332/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Taissir Boukrouba (taissir2002) \\ \uline{cheatography.com/taissir2002} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 19th November, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Introduction}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Pandas}} is a package built on top of NumPy, and provides an efficient implementation of many features : \newline % Row Count 3 (+ 3) - DataFrames \newline % Row Count 4 (+ 1) - Series \newline % Row Count 5 (+ 1) - Data Alignement \newline % Row Count 6 (+ 1) - Handling Missing Data \newline % Row Count 7 (+ 1) - Grouping and Aggregation \newline % Row Count 8 (+ 1) - Data Input and Output \newline % Row Count 9 (+ 1) - Handling Time Series% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.00694 cm} x{2.01388 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas General Methods}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Accessing values}} & \seqsplit{pd\_ds.values} & DataFrame.values\{\{nl\}\}Series.values \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{Accessing Indices}} & \seqsplit{pd\_ds.index} & DataFrame.index\{\{nl\}\}Series.index \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Accessing specific element}} & pd\_ds{[}idx{]} & DataFrame{[}1{]}\{\{nl\}\}Series{[}1{]} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{Accessing range of elements}} & pd\_ds{[}start : end{]} & DataFrame{[}1:4{]}\{\{nl\}\}Series{[}2:5{]} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Implicit Indexing}} & df.iloc{[}rows , cols{]} & data.iloc{[}1:3{]}\{\{nl\}\}\#last index is not included \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} {\bf{Explicit Indexing}} & df.loc{[}rows,cols{]} & data.loc{[}'California' : 'Texas'{]}\{\{nl\}\}\#last index is included \tn % Row Count 18 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.60195 cm} x{1.3731 cm} x{1.60195 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas Series}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Creating Series with lists}} & pd.Series({[}values{]}, index= list) & data = pd.Series({[}0.25, 0.5, 0.75, 1.0{]}, \{\{nl\}\}index={[}'a', 'b', 'c', 'd'{]}) \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{Creating Series with dictionaries}} & pd.Series(\{index:value\}) & \seqsplit{population\_dict} = \{'California': 38332521, 'Texas': 26448193, 'New York': 19651127, 'Florida': 19552860, 'Illinois': 12882135\}\{\{nl\}\}population = \seqsplit{pd.Series(population\_dict)} \tn % Row Count 19 (+ 13) % Row 2 \SetRowColor{LightBackground} {\bf{Slicing Series}} & Series{[}from\_idx : to\_idx{]} & population{[}'Texas':'Florida'{]} \tn % Row Count 22 (+ 3) % Row 3 \SetRowColor{white} {\bf{Slicing Indices with Dictionary Series}} & pd.Series(\{index:value\} , index={[}{]}) & pd.Series(\{2:'a', 1:'b', 3:'c'\}, index={[}3, 2{]}) \{\{nl\}\} \# only returns the third and second index respectfully \tn % Row Count 30 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{Pandas Series Index Can be a list of string or list of integers (or any desired type) unlike numpy arrays} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.19002 cm} x{1.69349 cm} x{1.69349 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas DataFrames}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Creating DataFrame}} & pd.DataFrame(\{index : iterable\}) & pd.DataFrame(\{'population': population, 'area': area\}) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Adding Column names}} & \seqsplit{pd.DataFrame(dict} , columns = {[}list\_of\_col\_names{]}) & \seqsplit{pd.DataFrame(population}, columns={[}'population'{]}) \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{Slicing DataFrame Index}} & \seqsplit{pd.DataFrame(dict} , columns = {[} {]} , index = {[} {]}) & \seqsplit{pd.DataFrame(np.random.rand(3}, 2), columns={[}'foo', 'bar'{]}, index={[}'a', 'b', 'c'{]}) \tn % Row Count 14 (+ 6) % Row 3 \SetRowColor{white} {\bf{Reading CSV Files}} & \seqsplit{pd.read\_csv(source} , index\_col = col) & \seqsplit{pd.read\_csv("data/president\_heights.csv"}, \seqsplit{index\_col="order")} \tn % Row Count 19 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{Saving DF to CSV}} & \seqsplit{dataframe.to\_csv(source)} & \seqsplit{df.to\_csv("data/president\_heights\_copy.csv")} \tn % Row Count 23 (+ 4) % Row 5 \SetRowColor{white} {\bf{Reading Excel Files}} & \seqsplit{pd.read\_excel(source)} & \seqsplit{pd.read\_excel("data/president\_heights.xlsx")} \tn % Row Count 27 (+ 4) % Row 6 \SetRowColor{LightBackground} {\bf{Saving DF to Excel}} & \seqsplit{dataframe.to\_excel(source)} & \seqsplit{df.to\_excel("data/president\_heights\_copy.xlsx")} \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.19002 cm} x{1.69349 cm} x{1.69349 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas DataFrames (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{Access DataFrame Columns}} & \seqsplit{dataframe.columns} & df.columns \tn % Row Count 3 (+ 3) % Row 8 \SetRowColor{white} {\bf{Transposing DataFrames}} & dataframe.T & df.T \tn % Row Count 6 (+ 3) % Row 9 \SetRowColor{LightBackground} {\bf{Subsetting Using loc}} & dataframe.loc{[}condition , cols{]} & data.loc{[}data.density \textgreater{} 100, {[}'pop', 'density'{]}{]} \tn % Row Count 10 (+ 4) % Row 10 \SetRowColor{white} {\bf{Masking}} & dataframe{[}mask{]} & data{[}data.density \textgreater{} 100{]} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.32733 cm} x{1.60195 cm} x{1.64772 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas Index}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Creating Index}} & \seqsplit{pd.Index(list)} & pd.Index({[}2, 3, 5, 7, 11{]}) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Accessing Index}} & Index{[}idx{]} & ind{[}1{]} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Slicing Index}} & Index{[}from : to : step{]} & ind{[} : : 2{]} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Intersection Between Indices}} & \seqsplit{index\_1.intersection(index\_2)} & indA = pd.Index({[}1, 3, 5, 7, 9{]})\{\{nl\}\}indB = pd.Index({[}2, 3, 5, 7, 11{]})\{\{nl\}\}indA.intersection(indB) \tn % Row Count 14 (+ 8) % Row 4 \SetRowColor{LightBackground} {\bf{Union Between Indices}} & \seqsplit{index\_1.union(index\_2)} & \seqsplit{indA.union(indB)} \tn % Row Count 17 (+ 3) % Row 5 \SetRowColor{white} {\bf{Symmetric Difference}} & index\_1. \seqsplit{symmetric\_difference(index\_2)} & \seqsplit{indA.symmetric\_difference(indB)} \tn % Row Count 20 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{The {\bf{Index}} has many of the attributes familiar from NumPy arrays such as : \newline ind.size, ind.shape, ind.ndim, ind.dtype} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Pandas Universal Functions}} \tn % Row 0 \SetRowColor{LightBackground} + & add() \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & sub() , subtract() \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & mul(), multiply() \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / & truediv(), div(), divide() \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} // & floordiv() \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \% & mod() \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} ** & pow() \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{These universal functions are used in the following form : \newline - \seqsplit{data\_struct.uf(data\_struct\_2)} \newline - data\_struct.uf()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Datatype Conversions (NaN or None)}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Float}} & No change \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{Object}} & No change \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {\bf{Integer}} & Upcast to float64 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} {\bf{Boolean}} & Upcast to object \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{These are data type conversion when there is missing values} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Operating On Missing Values}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Nullability Check}} & \seqsplit{data\_struc.is\_null()} & data = pd.Series({[}1, np.nan, 'hello', None{]})\{\{nl\}\}data.isnull() \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} {\bf{Non-Nullability Check}} & \seqsplit{data\_struc.not\_null()} & \seqsplit{data.not\_null()} \tn % Row Count 7 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Slicing Non-Null Values}} & data\_struct{[}data\_struc.not\_null(){]} & data{[}data.not\_null(){]} \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} {\bf{Dropping Null Values}} & \seqsplit{data\_struct.dropna(axis=0/1} , how = 'any'/'all' , thresh = n) & \seqsplit{data.dropna(axis} = 0 , thresh = 2)\{\{nl\}\}\# the tresh means each row has at least 2 non-null values \tn % Row Count 18 (+ 8) % Row 4 \SetRowColor{LightBackground} {\bf{Filling Missing Values}} & \seqsplit{data\_struct.fillna(value} , method = \seqsplit{'ffil'/'bfill'} , axis = 0/1) & \seqsplit{df.fillna(method='ffill'}, axis=1) \tn % Row Count 23 (+ 5) % Row 5 \SetRowColor{white} {\bf{Filling Using A Function (Interpolation)}} & \seqsplit{data\_struct.interpolate(method='linear'/'polynomial'...)} & df.interpolate()\{\{nl\}\}\# the method is linear by default \tn % Row Count 28 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{When Working with missing values methods , axis = 0 means rows and 1 columns} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas Multi-Indexing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Creating Multi-Index From Tuples}} & \seqsplit{pd.MultiIndex.from\_tuples(tuple)} & index = \seqsplit{pd.MultiIndex}.from\_tuples({[}('California', 2000), \seqsplit{('California'}, 2010){]}) \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} {\bf{Creating Multi-Index From Arrays}} & \seqsplit{pd.MultiIndex.from\_arrays(list)} & \seqsplit{pd.MultiIndex}.from\_arrays({[}{[}'a', 'a', 'b', 'b'{]}, {[}1, 2, 1, 2{]}{]}) \tn % Row Count 12 (+ 5) % Row 2 \SetRowColor{LightBackground} {\bf{Creating Multi-Index From Product}} & \seqsplit{pd.MultiIndex}.from\_product({[}index1\_list,index2\_list{]}) & \seqsplit{pd.MultiIndex}.from\_product({[}{[}'a', 'b'{]}, {[}1, 2{]}{]}) \tn % Row Count 17 (+ 5) % Row 3 \SetRowColor{white} {\bf{Creating Multi-Index From DataFrame Values}} & \seqsplit{pd.MultiIndex.from\_frame(dataframe)} & df = pd.DataFrame({[}{[}'a', 'b'{]}, {[}1, 2{]}{]})\{\{nl\}\}pd.MultiIndex.from\_frame(df) \tn % Row Count 23 (+ 6) % Row 4 \SetRowColor{LightBackground} {\bf{Applying Muli-Index}} & \seqsplit{data\_struct.reindex(index)} & pop = \seqsplit{pop.reindex(index)} \tn % Row Count 25 (+ 2) % Row 5 \SetRowColor{white} {\bf{Setting Index From Columns}} & data\_struct.set\_index({[}cols{]}) & pop\_flat.set\_index({[}'population'{]}) \tn % Row Count 28 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Accessing \seqsplit{Multi-Indexed} Data Structures}} & data\_struct{[}first\_index,second\_index,....., col{]} & pop{[}:, 2010{]}\{\{nl\}\}\# gets all rows from first index and only 2010 rows from second index \tn % Row Count 35 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas Multi-Indexing (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{Unstacking}} & \seqsplit{data\_struct.unstack()} & pop.unstack()\{\{nl\}\}\# this converts the last index (if we have 2 then the second one) values into cols \tn % Row Count 8 (+ 8) % Row 8 \SetRowColor{white} {\bf{Stacking}} & \seqsplit{data\_struct.stack()} & pop.stack()\{\{nl\}\}\# this converts columns into a second index \tn % Row Count 13 (+ 5) % Row 9 \SetRowColor{LightBackground} {\bf{Naming Multi-Indexes}} & \seqsplit{data\_struct.index.names} = list & \seqsplit{pop.index.names} = {[}'state', 'year'{]} \tn % Row Count 16 (+ 3) % Row 10 \SetRowColor{white} {\bf{Swapping Multi-Indexes}} & \seqsplit{data\_struct.swaplevel(0},1) & pop\_df = \seqsplit{pop\_df.swaplevel(0},1) \tn % Row Count 19 (+ 3) % Row 11 \SetRowColor{LightBackground} {\bf{Dropping Multi-Indexes}} & \seqsplit{data\_struct.droplevel(level=index)} & \seqsplit{pop\_df.droplevel(level=0)} \tn % Row Count 22 (+ 3) % Row 12 \SetRowColor{white} {\bf{Multi-Index In Columns}} & \seqsplit{pd.DataFrame(data}, \seqsplit{index=multi\_index\_rows}, \seqsplit{columns=multi\_indx\_cols)} & columns = \seqsplit{pd.MultiIndex}.from\_product({[}{[}'Bob', 'Guido', 'Sue'{]}, {[}'HR', 'Temp'{]}{]}, names={[}'subject', 'type'{]})\{\{nl\}\}health\_data = \seqsplit{pd.DataFrame(data}, index=index, \seqsplit{columns=columns)} \tn % Row Count 36 (+ 14) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Pandas Multi-Indexing (cont)}} \tn % Row 13 \SetRowColor{LightBackground} {\bf{Slicing Using Multi-Index Column Values}} & dataframe{[}multi\_ind\_col\_value{]} & health\_data{[}'Guido'{]} \tn % Row Count 4 (+ 4) % Row 14 \SetRowColor{white} {\bf{Slicing Multi-Index Cols \& Rows Using IndexSlice}} & idx = pd.IndexSlice\{\{nl\}\}df.loc{[}idx{[}index\_row1,index\_row2{]}, idx{[}index\_col1,index\_col2{]}{]} & idx = pd.IndexSlice\{\{nl\}\}health\_data.loc{[}idx{[}:, 1{]}, idx{[}:, 'HR'{]}{]} \tn % Row Count 11 (+ 7) % Row 15 \SetRowColor{LightBackground} {\bf{Resetting Multi-Index to Cols}} & \seqsplit{data\_struct.reset\_index()} & \seqsplit{pop.reset\_index(name='population')} \tn % Row Count 14 (+ 3) % Row 16 \SetRowColor{white} {\bf{Sorting Multi-Index}} & \seqsplit{data\_struct.sort\_index()} & \seqsplit{data.sort\_index()} \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{It is a good practice to sort the values after swapping Multi-index Levels} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Concatenation , Merging and Joins}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Concatenation}} & pd.concat({[}data\_struc , data\_struct2{]} , \seqsplit{ignore\_index} = True/False) & pd.concat({[}ser1, ser2{]}) \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{Adding MultiIndex Keys}} & pd.concat({[}data\_struc , data\_struct2{]} , keys = {[}'a','b'{]} ) & display('x', 'y', "pd.concat({[}x, y{]}, keys={[}'x', 'y'{]})") \tn % Row Count 11 (+ 5) % Row 2 \SetRowColor{LightBackground} {\bf{Concatenation with Joins}} & pd.concat({[}data\_struc , data\_struct2{]} , join = \seqsplit{'outer'/'inner'} ) & pd.concat({[}df5, df6{]}, join='inner')\{\{nl\}\}\# The intersection of 2 DFs \tn % Row Count 17 (+ 6) % Row 3 \SetRowColor{white} {\bf{Merging}} & \seqsplit{pd.merge(data\_struc} , \seqsplit{data\_struct2)} & df3 = pd.merge(df1, df2) \tn % Row Count 20 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Merging on Columns}} & \seqsplit{pd.merge(data\_struc} , \seqsplit{data\_struct2} , on \seqsplit{='col\_name')} & pd.merge(df1, df2, \seqsplit{on='employee')} \tn % Row Count 24 (+ 4) % Row 5 \SetRowColor{white} {\bf{Specific Merging}} & \seqsplit{pd.merge(data\_struc} , \seqsplit{data\_struct2} , right\_on ='col\_name' , left\_on \seqsplit{='col\_name')} & pd.merge(df1, df3, \seqsplit{left\_on="employee"}, \seqsplit{right\_on="name")}.drop('name', axis=1) \{\{nl\}\}\# When using right and left on we have to drop one of the cols (avoid redundancy) \tn % Row Count 37 (+ 13) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Concatenation , Merging and Joins (cont)}} \tn % Row 6 \SetRowColor{LightBackground} {\bf{Joining (Default Merge to indices only)}} & \seqsplit{data\_struct.join(data\_struct2)} & \seqsplit{df1a.join(df2a)} \tn % Row Count 4 (+ 4) % Row 7 \SetRowColor{white} {\bf{Merging on Indices}} & \seqsplit{pd.merge(data\_struc} , \seqsplit{data\_struct2} , \seqsplit{left\_index=True} , right\_index = True) & \seqsplit{pd.merge(df1a}, df3, \seqsplit{left\_index=True}, \seqsplit{right\_on='name')} \tn % Row Count 10 (+ 6) % Row 8 \SetRowColor{LightBackground} {\bf{Merging with methods}} & \seqsplit{pd.merge(data\_struct}, \seqsplit{data\_struct2}, \seqsplit{how='inner'/'outer'/'left'/'right')} & pd.merge(df6, df7, how='inner') \tn % Row Count 16 (+ 6) % Row 9 \SetRowColor{white} {\bf{Merging Conflicting Col Names}} & \seqsplit{pd.merge(data\_struct}, \seqsplit{data\_struct2}, suffixes = {[}'\_suff1', \_'suff2'{]}) & pd.merge(df8, df9, on="name", suffixes={[}"\_Sem1", "\_Sem\_2"{]}) \tn % Row Count 22 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{- Note that when adding multi-index keys in a concatenation , the number of keys should be the same as the number of data structures being concatenated} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.51041 cm} x{1.51041 cm} x{1.55618 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Advanced Group By Methods}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Aggregation using a list (General)}} & \seqsplit{df.groupby('col')}.aggregate({[}list\_of\_methods{]} & \seqsplit{df.groupby('key')}.aggregate({[}'min', np.median, max{]}) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Aggregation using a dict (Specific)}} & \seqsplit{df.groupby('col')}.aggregate(\{'col' : 'method' , 'col2' : 'method'\}) & \seqsplit{df.groupby('key')}.aggregate(\{'data1': 'min', 'data2': 'max'\}) \tn % Row Count 10 (+ 6) % Row 2 \SetRowColor{LightBackground} {\bf{Filtering}} & \seqsplit{df.groupby('col').filter(func)} & def \seqsplit{filter\_func(x):} \{\{nl\}\} return x{[}'data2'{]}.std() \textgreater{} 4\{\{nl\}\} \seqsplit{df.groupby('key').filter(filter\_func)} \tn % Row Count 18 (+ 8) % Row 3 \SetRowColor{white} {\bf{Transformation}} & \seqsplit{df.groupby('col').transform(lambda\_func)} & \seqsplit{df.groupby('key').transform(lambda} x: x - x.mean()) \tn % Row Count 22 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{Apply}} & \seqsplit{df.groupby('col').apply(user\_func)} & def norm\_by\_data2(x):\{\{nl\}\} \# x is a DataFrame of group values \{\{nl\}\}x{[}'data1'{]} /= x{[}'data2'{]}.sum()\{\{nl\}\} return x \{\{nl\}\}df.groupby('key').apply(norm\_by\_data2) \tn % Row Count 35 (+ 13) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.51041 cm} x{1.51041 cm} x{1.55618 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Advanced Group By Methods (cont)}} \tn % Row 5 \SetRowColor{LightBackground} {\bf{Grouping By Custom Mapping}} & \seqsplit{df.groupby(mapping).method} & mapping = \{'A': 'vowel', 'B': 'consonant', 'C': 'consonant'\}\{\{nl\}\}df2.groupby(mapping).sum() \tn % Row Count 8 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{aggregate()}} : this method allows using more than one function with groupby for different columns \newline {\bf{filter()}} : this method allows user-defined filter functions to be applied with a groupby (uses boolean operations only) \newline {\bf{transform()}}: mostly uses lambda functions to returned new and transformed version of a columns \newline {\bf{apply()}}: this method allows you to apply arbitrary user-defined functions with groupby} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}