\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{monika.g} \pdfinfo{ /Title (pandas-cheat-sheet.pdf) /Creator (Cheatography) /Author (monika.g) /Subject (Pandas 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}{A3570B} \definecolor{LightBackground}{HTML}{F9F4EF} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{monika.g} via \textcolor{DarkBackground}{\uline{cheatography.com/175674/cs/36874/}}} \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}monika.g \\ \uline{cheatography.com/monika-g} \\ \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 6th February, 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{To Start}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{import}} numpy {\bf{as}} np \newline {\bf{import}} pandas {\bf{as}} pd} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Create}} \tn % Row 0 \SetRowColor{LightBackground} `pd.DataFrame(\{\{nl\}\}dict/list, \{\{nl\}\}index = None, \{\{nl\}\}columns = None)` & \{\{width=50\}\}create DataFrame from list or dictionary \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} `df.index{[}names{]}` & set custom indexes \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} `pd.Series(\{\{nl\}\}list/np\_array/dict, \{\{nl\}\}index = None)` & create series from the list or np\_array or dictionary \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.36 cm} x{2.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Input and Output}} \tn % Row 0 \SetRowColor{LightBackground} `pd.read\_csv('name', \{\{nl\}\}index\_col = None)` & \{\{width=50\}\}read csv \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `pd.read\_excel('name')` & read excel \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} `df.to\_csv('name', \{\{nl\}\}index = False)` & save to csv \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} `df.to\_excel('name', \{\{nl\}\}'sheet\_name = 'name', \{\{nl\}\}index = False)` & save to excel \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Iteration}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for lab, row in df.{\bf{iterrrows()}}: \newline ~print(lab) \newline ~print(row)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Functions/Methods}} \tn % Row 0 \SetRowColor{LightBackground} `s.drop(row\_index, axis = 0)` & \{\{width=50\}\}drop values from rows of series \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `df.drop(col\_name, axis = 0)` & drop values from columns \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} `df.drop(columns={[}col\_names{]})` & drop columns from DataFrame \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`df.drop\_duplicates()`} & remove duplicate rows (only considers columns) \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} `df.sort\_index(by = col\_names)` & sort by the values along an axis \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} `df.sort\_values(\{\{nl\}\}by = col\_names, \{\{nl\}\}ascending = False)` & order rows by values of a column high to low \tn % Row Count 16 (+ 4) % Row 6 \SetRowColor{LightBackground} `df.rename(columns = \{\{nl\}\}\{'old\_name':'new\_name'\})` & rename the columns of a DataFrame \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} `df.rank()` & assign ranks to entries \tn % Row Count 21 (+ 2) % Row 8 \SetRowColor{LightBackground} `pd.concat({[}df1,df2{]})` & append rows of DataFrames \tn % Row Count 23 (+ 2) % Row 9 \SetRowColor{white} `len(df)` & number of rows in DataFrame \tn % Row Count 25 (+ 2) % Row 10 \SetRowColor{LightBackground} `df1.join(df2)` & join two DataFrames \tn % Row Count 26 (+ 1) % Row 11 \SetRowColor{white} `df{[}'col\_name'{]}.unique()` & return unique values from column \tn % Row Count 28 (+ 2) % Row 12 \SetRowColor{LightBackground} `df{[}col\_name{]}.apply(\{\{nl\}\}func/type.method)` & apply function to column \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Functions/Methods (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \seqsplit{`df.apply(func/type.method)`} & apply function \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Extract}} \tn % Row 0 \SetRowColor{LightBackground} `df{[}col\_names{]}` & \{\{width=50\}\}series of column \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `df{[}{[}col\_names{]}{]}` & select column \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} `df{[}start:end{]}` & select many columns \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `df.loc{[}index\_name{]}` & select row \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} `df.iloc{[}index\_num{]}` & select rows \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} `df.loc{[}{[}index\_names{]}{]}` & select rows \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} `df.iloc{[}{[}index\_nums{]}{]}` & select rows \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} `df.loc{[}{[}index\_names{]}, \{\{nl\}\}{[}col\_names{]}{]}` & select rows and columns \tn % Row Count 13 (+ 3) % Row 8 \SetRowColor{LightBackground} `df.loc{[}:, {[}col\_names{]}{]}` & select all rows and few columns \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} `df.iloc{[}:, {[}col\_nums{]}{]}` & select all rows and few columns \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} `df.head(n)` & select first n rows \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} `df.tail(n)` & select last n rows \tn % Row Count 19 (+ 1) % Row 12 \SetRowColor{LightBackground} `df.filter(regex = 'regex')` & select columns whose name matches regular expression regex \tn % Row Count 22 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Boolean Operators}} \tn % Row 0 \SetRowColor{LightBackground} `df{[}np.{\bf{logical\_and}}(\{\{nl\}\}con1, con2, ...){]}` & \{\{width=50\}\} 1 'and' 2 condition ... \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `df.loc{[}con1 {\bf{\&}} con2{]}` & 1 'and' 2 condition ... \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} `df{[}np.{\bf{logical\_or}}(\{\{nl\}\}con1, con2, ...){]}` & 1 'or' 2 condition ... \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} `df.loc{[}con1 {\bf{|}} con2{]}` & 1 'or' 2 condition ... \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} `df{[}np.{\bf{logical\_not}}(con){]}` & 'not' condition \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} `df.loc{[} {\bf{\textasciitilde{}}} con1{]}` & 'not' condition \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} `df{[}var{]}` & for condition \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.4 cm} x{5.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Get DataFrame Information}} \tn % Row 0 \SetRowColor{LightBackground} `df.shape` & \{\{width=50\}\}(rows, colums) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `df.index` & decribe index \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{`df.columns`} & describe DataFrame columns \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `df.info()` & info on DataFrame \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{`df.count()`} & number of non\_NA values for columns \tn % Row Count 6 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{`df.describe()`} & summary statics \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Math}} \tn % Row 0 \SetRowColor{LightBackground} `df.sum()` & \{\{width=50\}\}sum of values for columns \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `df.cumsum()` & cummulative sum of values \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `df.min() \{\{nl\}\} df.max()` & minimum values for columns \{\{nl\}\} maximum values for columns \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} `df.median()` & median of values columns \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} `df.mean()` & mean of values for columns \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `df.std()` & standard deviation of each object \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} `df.var()` & variance of each object \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}