\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{dydypd} \pdfinfo{ /Title (pandas-cheatsheet.pdf) /Creator (Cheatography) /Author (dydypd) /Subject (Pandas Cheatsheet 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 Cheatsheet Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{dydypd} via \textcolor{DarkBackground}{\uline{cheatography.com/201659/cs/42710/}}} \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}dydypd \\ \uline{cheatography.com/dydypd} \\ \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 18th March, 2024.\\ 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{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Change Value}} \tn % Row 0 \SetRowColor{LightBackground} Áp Dụng Nhiều Hàm với Pipe: & `\{\{lang-python\}\}df.pipe(tinh\_tong).pipe(bo\_cot, "a")` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Áp Dụng Hàm Cho Một Cột: & `\{\{lang-python\}\}df{[}"col1"{]} = df{[}"col1"{]}.apply(lambda row: row * 2)` \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} Áp Dụng Hàm Cho Từng Phần Tử: & `\{\{lang-python\}\}df.applymap(lambda val: "failed" if val \textless{} 5 else "passed")` \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} Gán Giá Trị Cho Nhiều Cột Mới: & `\{\{lang-python\}\}df = \seqsplit{df.assign(col3=lambda} x: x.col1 {\emph{ 100 + \seqsplit{x.col2).assign(col4=lambda} x: x.col2 }} x.col3)` \tn % Row Count 17 (+ 6) % Row 4 \SetRowColor{LightBackground} Thay Đổi Giá Trị của Series Bằng Dictionary: & `\{\{lang-python\}\}s.map(\{"a": 1, "b": 2, "c": 3\})` \tn % Row Count 20 (+ 3) % Row 5 \SetRowColor{white} Chuyển Mỗi Phần Tử Trong Iterable Thành Một Dòng: & `\{\{lang-python\}\}df.explode("a")` \tn % Row Count 24 (+ 4) % Row 6 \SetRowColor{LightBackground} Tách Chuỗi Thành Nhiều Dòng: & `\{\{lang-python\}\}df.a = df.a.str.split(",") \{\{nl\}\} df.explode("a")` \tn % Row Count 28 (+ 4) % Row 7 \SetRowColor{white} Forward Fill Trong pandas: & `\{\{lang-python\}\}df = \seqsplit{df.fillna(method="ffill")`} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Change Value (cont)}} \tn % Row 8 \SetRowColor{LightBackground} Thay Thế Giá Trị Trống Bằng Giá Trị Phổ Biến Nhất: & `\{\{lang-python\}\}df.fillna(df.mode().iloc{[}0{]})` \tn % Row Count 4 (+ 4) % Row 9 \SetRowColor{white} Mã Hóa Giá Trị Phân Loại Trong DataFrame: & `\{\{lang-python\}\}df{[}"encoded\_col1"{]} = df{[}"col1"{]}.astype("category").cat.codes` \tn % Row Count 8 (+ 4) % Row 10 \SetRowColor{LightBackground} Tối Ưu Hóa Mã với Phép Toán Vector: & `\{\{lang-python\}\}df = \seqsplit{df.fillna(fill\_dict)`} \tn % Row Count 11 (+ 3) % Row 11 \SetRowColor{white} Ném Ngoại lệ Cho Gán Gía Trị Liên Kết: & `\{\{lang-python\}\}pd.options.mode.chained\_assignment = "raise" df{[}df{[}"col1"{]} \textgreater{} 0{]}{[}"col2"{]} = 1` \tn % Row Count 16 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Truy Cập Dữ Liệu}} \tn % Row 0 \SetRowColor{LightBackground} Truy Cập Nhóm Các Dòng với loc và iloc: & `\{\{lang-python\}\}s.iloc{[}0{]} \{\{nl\}\}s.loc{[}20{]}` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} pd.Series.between: Chọn Các Dòng Chứa Giá Trị Trong Một Phạm Vi: & `\{\{lang-python\}\}s{[}s.between(0, 10){]}` \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{pandas.Series.pct\_change:} Tính Phần Trăm Thay Đổi Giữa Giá Trị Hiện Tại và Trước Đó: & `\{\{lang-python\}\}df.a.pct\_change()` \tn % Row Count 13 (+ 6) % Row 3 \SetRowColor{white} Tính Sự Khác Biệt Giữa Các Dòng của DataFrame: & `\{\{lang-python\}\}df.diff()` \tn % Row Count 16 (+ 3) % Row 4 \SetRowColor{LightBackground} DataFrame.diff và DataFrame.shift: Lấy Sự Khác Biệt Giữa Các Dòng Trong Một Cột: & `\{\{lang-python\}\}diff = df.diff() \{\{nl\}\}shift = diff.shift(-1)` \tn % Row Count 21 (+ 5) % Row 5 \SetRowColor{white} Chuyển pandas Series thành Mảng NumPy: & `\{\{lang-python\}\}s.values` \tn % Row Count 24 (+ 3) % Row 6 \SetRowColor{LightBackground} df.to\_dict: Chuyển DataFrame thành Từ Điển: & `\{\{lang-python\}\}df.to\_dict()` \tn % Row Count 27 (+ 3) % Row 7 \SetRowColor{white} Lấy Số Lần Xuất Hiện và Phần Trăm của Một Giá Trị Trong Một Cột: & `\{\{lang-python\}\}size.value\_counts() \{\{nl\}\}size.value\_counts(normalize=True)` \tn % Row Count 32 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Truy Cập Dữ Liệu (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \seqsplit{pandas.DataFrame.corrwith:} Tính Tương Quan Giữa 2 DataFrame: & `\{\{lang-python\}\}df1.corrwith(df2)` \tn % Row Count 4 (+ 4) % Row 9 \SetRowColor{white} pandas.cut: Chia Giá Trị của DataFrame thành Các Khoảng Rời Rạc: & `\{\{lang-python\}\}pd.cut(df{[}"a"{]}, bins=bins)` \tn % Row Count 8 (+ 4) % Row 10 \SetRowColor{LightBackground} pandas.qcut: Chia Giá Trị của DataFrame thành Các Khoảng Có Số Lượng Phần Tử Bằng Nhau: & `\{\{lang-python\}\}pd.qcut(df{[}"a"{]}, q=3)` \tn % Row Count 14 (+ 6) % Row 11 \SetRowColor{white} DataFrame.cumsum: Lấy Tổng Cộng Tích Lũy qua Mỗi Cột: & `\{\{lang-python\}\}df.cumsum()` \tn % Row Count 18 (+ 4) % Row 12 \SetRowColor{LightBackground} \seqsplit{pandas.DataFrame.cummax:} Lấy Giá Trị Tối Đa Tích Lũy: & `\{\{lang-python\}\}nums.cummax()` \tn % Row Count 22 (+ 4) % Row 13 \SetRowColor{white} Lấy Tổng Của Tất Cả Cột Trong DataFrame: & `\{\{lang-python\}\}df.sum(axis=1) \{\{nl\}\}df.sum(axis=0)` \tn % Row Count 25 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Làm Việc với Datetime}} \tn % Row 0 \SetRowColor{LightBackground} Chuyển Đổi Các Cột thành Đối Tượng Thời Gian Khi Sử Dụng pandas để Đọc Các Tệp CSV: & `\{\{lang-python\}\}pd.read\_csv("data.csv", parse\_dates={[}"date\_column\_1", "date\_column\_2"{]})` \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} pandas' DateOffset: Thêm Khoảng Thời Gian vào Một Điểm Thời Gian của pandas: & `\{\{lang-python\}\}ts + \seqsplit{DateOffset(months=3)} ts + DateOffset(years=3, hours=3) ts + BDay(n=6)` \tn % Row Count 11 (+ 5) % Row 2 \SetRowColor{LightBackground} DataFrame rolling: Tính Trung Bình Của n Dữ Liệu Trước Đó Sử Dụng pandas: & `\{\{lang-python\}\}df.rolling(3).mean()` \tn % Row Count 16 (+ 5) % Row 3 \SetRowColor{white} pandas Grouper: Nhóm Giá Trị Dựa trên Một Tần Suất Cụ Thể: & `\{\{lang-python\}\}df.groupby(pd.Grouper(key="date", freq="1W")).mean()` \tn % Row Count 20 (+ 4) % Row 4 \SetRowColor{LightBackground} pandas.Series.dt: Truy Cập Các Thuộc Tính Thời Gian của Một Series của pandas: & `\{\{lang-python\}\}df{[}"date"{]}.dt.year df{[}"date"{]}.dt.time` \tn % Row Count 25 (+ 5) % Row 5 \SetRowColor{white} Lấy Các Dòng trong Một Phạm Vi Năm: & `\{\{lang-python\}\}df.loc{[}"2019":{]}` \tn % Row Count 28 (+ 3) % Row 6 \SetRowColor{LightBackground} pandas.reindex: Thay Thế Giá Trị Của Các Ngày Thiếu Trong Một Dãy Ngày Bằng 0: & `\{\{lang-python\}\}new\_s = \seqsplit{s.reindex(new\_index}, fill\_value=0)` \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Làm Việc với Datetime (cont)}} \tn % Row 7 \SetRowColor{LightBackground} Chọn Các Dòng Của DataFrame Trước hoặc Sau Một Ngày Cụ Thể: & `\{\{lang-python\}\}df{[}df.date \textless{}= "2021-07-21"{]}` \tn % Row Count 4 (+ 4) % Row 8 \SetRowColor{white} resample: Tái Mẫu Dữ Liệu Chuỗi Thời Gian: & `\{\{lang-python\}\}s.resample('2D').sum()` \tn % Row Count 7 (+ 3) % Row 9 \SetRowColor{LightBackground} Dịch Chỉ Số Của DataFrame Theo Số Kỳ Hạn Cụ Thể: & `\{\{lang-python\}\}df.shift(periods=1)` \tn % Row Count 11 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}