https://www.datacamp.com/community/blog/python-pandas-cheat-sheet
The Pandas library is built on NumPy and provides easy-to-use data structures and data analysis tools for the Python programming language.
Use the following import convention:
import pandas as pd
pd.__version__
A one-dimensional labeled array A capable of holding any data type
s = pd.Series([3, -5, 7, 4], index=['a', 'b', 'c', 'd'])
s
A two-dimensional labeled data structure with columns of potentially different types
data = {'Country': ['Belgium', 'India', 'Brazil'],
'Capital': ['Brussels', 'New Delhi', 'Brasilia'],
'Population': [11190846, 1303171035, 207847528]}
df = pd.DataFrame(data,
columns=['Country', 'Capital', 'Population'])
df
help(pd.Series.loc)
# Get one element
s['b']
# Get subset of a DataFrame
df[1:]
## Select single value by row & column
df.iloc[0, 0]
# Select single value by row & column
df.iat[0, 0]
# Select single value by row & column labels
df.loc[0, 'Country']
# Select single value by row & column labels
df.at[0, 'Country']
# Select single row of subset of rows
df.ix[2]
# Select a single column of subset of columns
df.ix[:,'Capital']
# Select rows and columns
df.ix[1,'Capital']
# Series s where value is not >1
s[~(s > 1)]
# s where value is <-1 or >2
s[(s < -1) | (s > 2)]
# Use filter to adjust DataFrame
df[df['Population'] > 1200000000]
# Set index a of Series s to 6
s['a'] = 6
# Drop values from rows (axis=0)
s.drop(['a', 'c'])
# Drop values from columns(axis=1)
df.drop('Country', axis=1)
# Sort by labels along an axis
df.sort_index()
# Sort by the values along an axis
df.sort_values(by='Country')
# Assign ranks to entries
df.rank()
# (rows,columns)
df.shape
# Describe index
df.index
# Describe DataFrame columns
df.columns
# Info on DataFrame
df.info()
# Number of non-NA values
df.count()
# Sum of values
df.sum()
# Cummulative sum of values
df.cumsum()
# Minimum values
df.min()
# Maximum values
df.max()
# Minimum index value
df.idxmin()
# Maximum index value
df.idxmax()
# Summary statistics
df.describe()
# Mean of values
df.mean()
# Median of values
df.median()
f = lambda x: x*2
# Apply function
df.apply(f)
# Apply function element-wise
df.applymap(f)
NA values are introduced in the indices that don’t overlap:
s3 = pd.Series([7, -2, 3], index=['a', 'c', 'd'])
s + s3
You can also do the internal data alignment yourself with the help of the fill methods:
s.add(s3, fill_value=0)
s.sub(s3, fill_value=2)
s.div(s3, fill_value=4)
s.mul(s3, fill_value=3)
df.to_csv('myDataFrame.csv')
pd.read_csv('myDataFrame.csv', header=None, nrows=5)
df.to_excel('myDataFrame.xlsx', sheet_name='Sheet1')
pd.read_excel('myDataFrame.xlsx')
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
write records stored in a DataFrame to a SQL database.
df.to_sql("my_table", engine)
read_sql() is a convenience wrapper around read_sql_table() and read_sql_query()
pd.read_sql("SELECT * FROM my_table;", engine)
pd.read_sql_table('my_table', engine)
pd.read_sql_query("SELECT * FROM my_table;", engine)