\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{DarioPittera (aggialavura)} \pdfinfo{ /Title (python-pandas.pdf) /Creator (Cheatography) /Author (DarioPittera (aggialavura)) /Subject (Python - 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}{FFA500} \definecolor{LightBackground}{HTML}{FFF9EF} \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{Python - Pandas Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{DarioPittera (aggialavura)} via \textcolor{DarkBackground}{\uline{cheatography.com/83764/cs/19829/}}} \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}DarioPittera (aggialavura) \\ \uline{cheatography.com/aggialavura} \\ \uline{\seqsplit{www}.dariopittera.com} \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 24th June, 2019.\\ 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}{TO START}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import numpy as np \newline import pandas as pd} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{SERIES (similar to numpy array)}} \tn % Row 0 \SetRowColor{LightBackground} pd.Series(data = list)\{\{nobreak\}\} & create series from list \{\{nobreak\}\} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{pd.Series(data=list,\{\{nl\}\} index=labels)}} & create series \{\{nl\}\} with index \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} pd.Series(np\_arr) & create series from \{\{nl\}\} numpy array \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} pd.Series(np\_arr,\{\{nl\}\} labels) & create series \{\{nl\}\} with index \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} pd.Series(dict) & create series \{\{nl\}\} from dictionary \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} {\bf{pd.Series{[}num{]}}} & indexing \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} ser1 + ser2 & sum two Series \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Pandas series}} differs from numpy arrays because series {\bf{can have axis labels}}, instead of just a number location. It also {\bf{doesn't need to hold numeric data}}, it can hold any arbitrary Python Object, also functions (although unlikely used). \newline \newline {\bf{Note:}} the terms "data=" and "index=" can be omitted.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{DATAFRAMES and INDEXING}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{df = pd.DataFrame()}} *\{\{nobreak\}\} & create dataframe \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{df{[}'col'{]}}} * & select col \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} {\bf{df.loc{[}'row'{]}}} & select row \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} df.iloc{[}'row'{]} & select a row \{\{nl\}\} by its index \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} df.col & select a column \{\{nl\}\} (opt.2 - avoid) \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} df{[}{[}'col1','col2'{]}{]} * & take two columns \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{type(df{[}'col'{]})}} & column type \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} {\bf{df{[}'new\_col'{]} = {[}1,2,3{]}}} & insert column \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} df.drop('row',axis=0)* & drop row \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} df.drop('col',axis=1) & drop column \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} df.drop('col',axis=1,\{\{nl\}\}inplace=True)* & permanent drop \tn % Row Count 20 (+ 3) % Row 11 \SetRowColor{white} {\bf{df.loc{[}'row1','col1'{]}}} & select a row \{\{nl\}\} and a column \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} {\bf{df.loc{[}{[}'r1', 'r2'{]},{[}'c1','c2'{]}{]}* }}\{\{nobreak\}\} & select 2 rows \{\{nl\}\} and 2 columns \tn % Row Count 25 (+ 3) % Row 13 \SetRowColor{white} df\textgreater{}condition & return boolean \tn % Row Count 26 (+ 1) % Row 14 \SetRowColor{LightBackground} df{[}df\textgreater{}cond{]} & return values \tn % Row Count 27 (+ 1) % Row 15 \SetRowColor{white} {\bf{df{[}df{[}'col'{]}\textgreater{}0{]}}} & return rows of col that satisfy condition \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{DATAFRAMES and INDEXING (cont)}} \tn % Row 16 \SetRowColor{LightBackground} df{[}df{[}'col1'{]}\textgreater{}0{]}{[}'col2'{]} & return col2 that satisfy cond. \{\{nl\}\} on col1 \tn % Row Count 3 (+ 3) % Row 17 \SetRowColor{white} df{[}df{[}'c1'{]}\textgreater{}0{]}{[}{[}'c2','c3'{]}{]} & return c2 \& c3 \{\{nl\}\} that satisfy \{\{nl\}\} cond. on col1 \tn % Row Count 6 (+ 3) % Row 18 \SetRowColor{LightBackground} df{[}(cond1) \& (cond2){]} & return values that satisfy cond1 \& cond2 \tn % Row Count 8 (+ 2) % Row 19 \SetRowColor{white} df{[}(cond1) | (cond2){]} & return values that satisfy cond1 | cond2 \tn % Row Count 10 (+ 2) % Row 20 \SetRowColor{LightBackground} {\bf{df.reset\_index()}} & add num index \tn % Row Count 11 (+ 1) % Row 21 \SetRowColor{white} df{[}'new\_col'{]} = 'NY LA'\{\{nl\}\}.split() & add col quickly \tn % Row Count 13 (+ 2) % Row 22 \SetRowColor{LightBackground} df.set\_index('col') & set a column \{\{nl\}\} as index \tn % Row Count 15 (+ 2) % Row 23 \SetRowColor{white} df.set\_index('col',\{\{nl\}\} inplace=True) & make it permanent \tn % Row Count 17 (+ 2) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{!!THERE IS ALSO MULTI-INDEXING"} \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{DataFrame}} function take a {\bf{data}} (the values), {\bf{index}} (the name of the index column), {\bf{columns}} (the name of the column) parameters. \newline \newline {\bf{Columns}} are series. \newline \newline {\bf{take two columns}}: note the double brackets {[}{[} {]}{]} \newline \newline {\bf{axis=0}} can be omitted, is the default value. \newline \newline {\bf{inplace=True}} will apply the result to the original dataframe. Without it, you are not changing the dataframe. \newline \newline {\bf{r}} = row. \newline {\bf{c}} = column.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.53827 cm} x{2.43873 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{MISSING DATA}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{df.isnull()}} & check for na \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{df.dropna()}} & drop all rows with \{\{nl\}\} at least 1 na \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} df.dropna(axis=1)\{\{nobreak\}\} & drop all cols with \{\{nl\}\} at least 1 na \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} df.dropna(thresh=n)\{\{nobreak\}\} & keep with at least \{\{nl\}\} n value/s \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{df.fillna(value='value')}}\{\{nobreak\}\} & replace na \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} df{[}'col'{]}.fillna(value=\{\{nl\}\}df{[}'col'{]}.mean()) & replace using funct \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{GROUPBY}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{df.groupby('col')}} & group rows by a col \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} grouped\_df{\bf{.count()}} & use cnt function \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} grouped\_df{\bf{.mean()}} & use mean function \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} grouped\_df.std() & use std function \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} grouped\_df.min() & use min function \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} grouped\_df.max() & use max function \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} grouped\_df{\bf{.describe()}} & df descriptives \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} grouped\_df('col')\{\{nl\}\}.count().loc{[}'row{]} & apply function and \{\{nl\}\} take a row \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} ... {\bf{.transpose()}} & rotate results \tn % Row Count 13 (+ 1) % Row 9 \SetRowColor{white} ... .transpose(){[}'row'{]} & rotate and \{\{nl\}\} take a row \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.83689 cm} x{2.14011 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{MERGING, JOINING, CONCATENATING}} \tn % Row 0 \SetRowColor{LightBackground} pd.concat({[}df1,df2,df3{]}) & concatenate dfs \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} pd.concat({[}...{]},axis=1) & concatenate by col \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} pd.merge()* & merge two dfs \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} df1.join(df2) & join two dfs \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{pd.merge()}} takes "{\bf{df1}}", "{\bf{df2}}", "{\bf{how=}}", "{\bf{on=}}" parameters. "how=" can be \seqsplit{"inner"/"outer"/"left"/"right"}, "on=" has to be a column/s key. \newline \newline {\bf{join()}} is similar to merge but works on indexes that can be different. It also cn take the "how=" argument.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{METHODS and FUNCTIONS}} \tn % Row 0 \SetRowColor{LightBackground} df{[}'c'{]}{\bf{.unique()}} & return unique values \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} df{[}'c'{]}{\bf{.nunique()}} & count unique val \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} df{[}'c'{]}{\bf{.value\_counts()}}\{\{nobreak\}\} & count how many \{\{nl\}\} of same values \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} df{[}'c1'{]}{\bf{.apply(func)}} & apply func to df \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} df{[}'c1'{]}.apply(len) & apply len \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} df{[}'c1'{]}.apply(sum) & apply sum \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} ... {\bf{.apply(lambda x:x+2)}}\{\{nobreak\}\} & apply lambda \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} df.index & return idx names \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} {\bf{df.info()}} & return df info \tn % Row Count 14 (+ 1) % Row 9 \SetRowColor{white} {\bf{df.describe()}} & return df stats \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} {\bf{df.columns}} & return col names \tn % Row Count 16 (+ 1) % Row 11 \SetRowColor{white} {\bf{del df{[}'col'{]}}}* & delete col from df \tn % Row Count 17 (+ 1) % Row 12 \SetRowColor{LightBackground} {\bf{df.sort\_values(by='col')}} & sort df values \tn % Row Count 19 (+ 2) % Row 13 \SetRowColor{white} df.pivot\_table()* & create a pivot tbl \tn % Row Count 20 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{del}} differs from ".drop()" because it will permanently remove a column from the df. \newline \newline {\bf{pivot\_table()}} takes "{\bf{values=}}", "{\bf{index=}}", "{\bf{columns=}}" parameters. It reads: "Create a table from df, with values of colx, index of colx2, and divided by values in colx3"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{INPUT and OUPUT code to start}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# to import HTML tables \newline conda install lxml \newline conda install html5lib \newline conda install BeautifulSoup4 \newline \newline \# to use SQL \newline from sqlalchemy import create\_engine \newline engine = \seqsplit{create\_engine('sqlite:///:memory:')} \newline df.to\_sql('data', engine) \newline sql\_df = pd.read\_sql('data',con=engine)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.83229 cm} x{1.14471 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{INPUT and OUPUT operations}} \tn % Row 0 \SetRowColor{LightBackground} pwd & ask nb route \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} df = {\bf{pd.read\_csv('example')}} & read csv \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} df = {\bf{pd.read\_excel\{\{nl\}\}}}('name',sheet\_name='name') & read excel \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} df = pd.read\_html('address') & read html \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} df{\bf{.to\_csv('str',index=False)\{\{nobreak\}\}}} & save as csv \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} df{\bf{.to\_excel}}('name',\{\{nl\}\}sheet\_name='name', \{\{nl\}\}index = False) & save as xlsx \tn % Row Count 12 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}