-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguide.tex
More file actions
130 lines (120 loc) · 3.68 KB
/
guide.tex
File metadata and controls
130 lines (120 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1.00in]{geometry}
\usepackage{amssymb,amsmath}
\setlength{\parskip}{.75em}
\begin{document}
\title{%
Python Data Analysis Guide \\
}
\author{Ayman Ali}
\date{}
\maketitle
% Table of Contents
\tableofcontents
\clearpage
\section{Dataframe Manipulation with Pandas}
\begin{enumerate}
\item Create dataframes based on value in column:
\begin{verbatim}
new_df = df.loc[df['Column'] == value]
\end{verbatim}
\item Drop columns:
\begin{verbatim}
drop_columns = ['Column 1', 'Column 2', 'Column 3']
new_df = df.drop(drop_columns, axis = 1)
\end{verbatim}
\item Select rows by index (works with slicing as well):
\begin{verbatim}
rows = df.iloc[index]
\end{verbatim}
\item Select row and column by index (can use slicing on both):
\begin{verbatim}
new_df = df.iloc[index_of_row, index_of_column]
\end{verbatim}
\item Replace values:
\begin{verbatim}
new_df = df.replace(to_replace = value_to_replace,
value = value_to_replace_with)
\end{verbatim}
\end{enumerate}
\section{Graphing with Matplotlib}
\subsection{General Setup}
\begin{enumerate}
\item For the imports:
\begin{verbatim}
import matplotlib.pyplot as plt
# if you want to see what styles you can pick from
plt.style.available
# personal preference
plt.style.use('bmh')
\end{verbatim}
\item Set up a graph that you're about to plot with:
\begin{verbatim}
'''
If both columns and rows are greater than 1, then ax will be a (row, column)
dimensional array. Otherwise, it'll be a one-dimensional array, with either
(rows) or (columns) depending on which one is greater than 1. You can access
each axis with standard indexing (i.e. ax[1][2] will give you the axes of
the graph in row 2 and column 3)
'''
fig, ax = plt.subplots(nrows = num_rows, ncols = num_cols,
sharex = boolean, sharey = boolean, dpi = integer,
figsize = (width, height))
\end{verbatim}
\item Titles:
\begin{verbatim}
# Individual axis titles with
ax.set_title('string')
# For a general graph title, use text boxes (next section)
\end{verbatim}
\item Adding text boxes to the figure:
\begin{verbatim}
# Find all kwargs at the following URL:
# https://matplotlib.org/api/text_api.html#matplotlib.text.Text
# Some of the more useful kwargs that I use are:
# verticalalignment ('center', 'top', 'bottom', 'baseline') and
# horizontalalignment ('center', 'right', 'left')
fig.text(double_xloc, double_yloc, 'text', **kwargs)
\end{verbatim}
\item Finishing the plot:
\begin{verbatim}
# If you had a label anywhere then make sure to call it
plt.legend(fontsize = integer or None)
plt.savefig(file_path)
plt.close()
plt.clf()
\end{verbatim}
\end{enumerate}
\subsection{Plot Types}
\begin{enumerate}
\item Standard plot:
\begin{verbatim}
# fill in
\end{verbatim}
\item Scatter:
\begin{verbatim}
# fill in
\end{verbatim}
\item Bar graphs:
\begin{verbatim}
spacing = double_val
plt.bar(x_values +/-/nothing spacing, y_vals, width = spacing,
align = 'center', color = any matplotlib color)
\end{verbatim}
\item Errorbars:
\begin{verbatim}
axis.errorbar(x_vals, y_vals, xerr = error_x, yerr = error_y,
markersize = integer,
lolims / uplims / xlolims / xuplims = optional booleans)
\end{verbatim}
\end{enumerate}
\subsection{Misc.}
\begin{enumerate}
\item Custom tick marks and steps:
\begin{verbatim}
plt.xticks(np.arange(start = start_val, stop = stop_val, step = step_val))
plt.yticks(np.arange(start = start_val, stop = stop_val, step = step_val))
\end{verbatim}
\end{enumerate}
\end{document}