\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{Molly\_6075} \pdfinfo{ /Title (pandas.pdf) /Creator (Cheatography) /Author (Molly\_6075) /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}{52A382} \definecolor{LightBackground}{HTML}{F4F9F7} \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}{Molly\_6075} via \textcolor{DarkBackground}{\uline{cheatography.com/193986/cs/40407/}}} \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}Molly\_6075 \\ \uline{cheatography.com/molly-6075} \\ \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 16th October, 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{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Basic Function}} \tn % Row 0 \SetRowColor{LightBackground} df = \seqsplit{pd.read\_csv("filename")} & 读取csv文件 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} df.info( ) & \seqsplit{返回每一列的非空行数、数据类型} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} df.dtypes & \seqsplit{返回每个字段的类型} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} df.describe( ) & \seqsplit{返回每一列的数量、均值、方差、最值、四分位值} \tn % Row Count 11 (+ 4) % Row 4 \SetRowColor{LightBackground} df.columns & 返回所有列名 \tn % Row Count 12 (+ 1) % Row 5 \SetRowColor{white} df.head(5) & \seqsplit{返回前5行的全部信息} \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} df.columns = {[}each.stripe( ), for each in df.columns{]} & \seqsplit{去除列名前后的空格,并更新列名} \tn % Row Count 17 (+ 3) \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}{Basic Calculation on One Column}} \tn % Row 0 \SetRowColor{LightBackground} df.loc{[}:,'columnName'{]}.std( ) & \seqsplit{返回某一列的标准差}.var( )表示方差 \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} df.loc{[}:,'columnName'{]}.mean() & \seqsplit{返回某一列的均值} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} df.ID.unique() & \seqsplit{返回ID列的所有不重复值} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} df.ID.nunique() & \seqsplit{不重复的ID值数量} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} df.loc{[}:,'columnName'{]}.max() & \seqsplit{某一列的最大值} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} df{[}'columnName'{]}.mode() & 某一列的众数 \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{df.ColumnName.value\_counts()} & \seqsplit{分组统计某一列不同值的数量,并从高到低排序} \tn % Row Count 17 (+ 4) % Row 7 \SetRowColor{white} df.ColumnName.value\_counts(){[}:1{]} & \seqsplit{取数量最大的一行} \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} \seqsplit{df.groupby('col1').col2.sum()} & \seqsplit{根据col1的值分组,统计col2的和} \tn % Row Count 22 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{df.groupby("col1").col2.mean().sort\_values()} & \seqsplit{分组后取均值,并由低到高排序} \tn % Row Count 25 (+ 3) % Row 10 \SetRowColor{LightBackground} \seqsplit{df.groupby("month").emp\_combined\_inclow.mean().sort\_values(ascending=False)} & \seqsplit{分组后取均值,由高到低排序} \tn % Row Count 29 (+ 4) % Row 11 \SetRowColor{white} \seqsplit{df.groupby("col1").col2.mean()}.sort\_values(){[}:1{]} & \seqsplit{排序后取第一个值(最小值)} \tn % Row Count 32 (+ 3) \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 new column/ Extract column}} \tn % Row 0 \SetRowColor{LightBackground} df{[}'NewColumn'{]} = \seqsplit{df.ColumnName.apply(lambda} x:"No" if x != 0 else "Yes") & \seqsplit{将某一列的值逻辑判断的结果存放在新列} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} df{[}'NewColumn'{]} = pd.cut(df.BMI,bins = {[}a,b,c,d,e{]}, labels={[}"label1", "label2", "label3", "4"{]}) & \seqsplit{根据某列值的返回添加标签(a至b属于label1)} \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} df\_a = df{[}df.Column3.isna() == False{]}.loc{[}:, {[}"Column1","Column2"{]}{]} & \seqsplit{依据条件提取某两列组成新的dataframe} \tn % Row Count 13 (+ 4) % Row 3 \SetRowColor{white} df = \seqsplit{df.drop('ColumnName'},axis = 1) & 删除某一列 \tn % Row Count 15 (+ 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}{Time-related manupulation}} \tn % Row 0 \SetRowColor{LightBackground} df = \seqsplit{pd.read\_csv("filename"},parse\_dates={[}'Date'{]}) & \seqsplit{读取文件的同时将Date列转化成指定日期格式} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} df = \seqsplit{pd.read\_csv("employment\_rate.csv"},parse\_dates={[}{[}"year","month","day"{]}{]}) & \seqsplit{把分别存放年、月、日的三列合并为同一列} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} df{[}'year'{]} = df.Date.dt.year & \seqsplit{提取Date列的年份存放在新列year} \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} df{[}'month{]} = df.Date.dt.month & \seqsplit{提取Date列的月份存放在新列month} \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} df{[}'day'{]} = df.Date.dt.day & \seqsplit{提取Date列的日期存放在新列day} \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} df{[}'day\_name'{]} = \seqsplit{df.Date.dt.day\_name()} & \seqsplit{根据Date计算星期信息存放在新列day\_name} \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} dfa{[}'Date'{]} = {[}datetime.strptime(x, "\%b-\%y") for x in df.Period{]} & \seqsplit{把形式为Jan-2020的日期列转化成标准日期的格式} \tn % Row Count 22 (+ 4) \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}{Correlation Analysis}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{返回所有列和目标列的相关系数,并按从低到高排序(所有列必须都是数值型,反映两个dataframe之间的相关关系)} & \seqsplit{df.corrwith(df.targetColumn).sort\_values()} \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \seqsplit{例子:指定列与目标列的相关系数,并排序} & selected\_col ={[}"a","b","c"{]} \tn % Row Count 11 (+ 3) % Row 2 \SetRowColor{LightBackground} & df{[}selected\_col{]}.corrwith(df.targetColumn).sort\_values() \tn % Row Count 14 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{一个dataframe中,所有列之间的两两相关关系} & df{[}selected\_col{]}.corr(method="spearman") \tn % Row Count 17 (+ 3) % Row 4 \SetRowColor{LightBackground} & df.loc{[}:, {[}"a", "b","c"{]}{]}.corr() \tn % Row Count 19 (+ 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}{Plot}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{df.groupby('column1').column2.mean().plot(kind} = 'bar') & 柱状图 \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{df.column1.plot(kind} = 'hist') & \seqsplit{频数分布直方图} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} df.plot(kind="line", y="column1", x="column2") & 普通折线图 \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{df.groupby("column3").column1.mean(} ).plot( ) & \seqsplit{分组统计后的折线图} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} df.groupby("column4"){[}{[}"column1","column2","column3"{]}{]}.mean().plot() & \seqsplit{多个列用相同标准分组,并将折线展示在同一个图中} \tn % Row Count 15 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Complex Plot}} \tn % Row 0 \SetRowColor{LightBackground} from pandas.plotting import lag\_plot & \seqsplit{绘制时间序列数据的滞后图} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} lag\_plot(df, lag = 2) & 默认的滞后期=1,如果数据存在自相关性,将沿着某条对角线分布 \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{from pandas.plotting import*} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{autocorrelation\_plot(df)} & \seqsplit{观察时间序列数据中的自相关性} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}