Skip to content
Python Personal Index
# Start coding here...
PYTHON PERSONAL INDEX
Introduction Python
-
Python as a calculator
# Addition, subtraction
print(5 + 5)
print(5 - 5)
# Multiplication, division, modulo, and exponentiation
print(3 * 5)
print(10 / 2)
print(18 % 7)
print(4 ** 2)
# How much is your $100 worth after 7 years?
print(100*1.1**7)
-
Functions:
print()
type()
int()
float()
bool()
len()
help()
max()
pow()
sorted(): sorted(, reverse=)
-
Methods
x.upper()
x.count()
x.index()
x.remove()
x.reverse()
Data Manipulation with Pandas
.head(): returns the first few rows (the “head” of the DataFrame).
.info(): shows information on each of the columns, such as the data type and number of missing values.
.shape: returns the number of rows and columns of the DataFrame.
.describe(): calculates a few summary statistics for each column.
.values: A two-dimensional NumPy array of values.
.columns: An index of columns: the column names.
.index: An index for the rows: either row numbers or row names.
.sort_values()
-
df.sort_values(""): one column
-
df.sort_values(["", ""]):multiple column
Subsetting columns
-
df["col_a"]
-
df[["col_a", "col_b"]]
Subsetting rows
-
df[df["x"] > 60]
-
df[df["y"] == "tan"]
-
df[(df["x"] > 60) & (df["y"] == "tan")]
Subsetting rows by categorical variables
-
df[(df["col"] == "value_1") | (df["col"] == "value_2")]
-
df[df["col"].isin(list)]
Summarizing numerical data
-
median() , .mode()
-
.min() , .max()
-
.var() , .std()
-
.sum()
-
.quantile()
Aggregations
-
def function(column):
-
return column.quantile(0.3)
-
df['column'].agg(function)
Cumulative statistics
-
.cummax()
-
.cummin()
-
.cumprod()