4. Pandas Introduction

Todo

convert tables to CSV

Pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis / manipulation tool available in any language. It is already well on its way toward this goal. Here are just a few of the things that pandas does well:

  • Easy handling of missing data (represented as NaN) in floating point as well as non-floating point data
  • Size mutability: columns can be inserted and deleted from DataFrame and higher dimensional objects
  • Automatic and explicit data alignment: objects can be explicitly aligned to a set of labels, or the user can simply ignore the labels and let Series, DataFrame, etc. automatically align the data for you in computations
  • Powerful, flexible group by functionality to perform split-apply-combine operations on data sets, for both aggregating and transforming data
  • Make it easy to convert ragged, differently-indexed data in other Python and NumPy data structures into DataFrame objects
  • Intelligent label-based slicing, fancy indexing, and subsetting of large data sets
  • Intuitive merging and joining data sets
  • Flexible reshaping and pivoting of data sets
  • Hierarchical labeling of axes (possible to have multiple labels per tick)
  • Robust IO tools for loading data from flat files (CSV and delimited), Excel files, databases, and saving/loading data from the ultrafast HDF5 format
  • Time series-specific functionality: date range generation and frequency conversion, moving window statistics, moving window linear regressions, date shifting and lagging, etc.

Podstawowymi strukturami danych w Pandas jest Series (seria) i DataFrame (obiekt tabeli); zobacz dokumentacje po więcej informacji.

import pandas as pd
import numpy as np

4.1. TL;DR

Tab. 4.4. Pandas most commonly used functions
Method Description
DataFrame.at() Access a single value for a row/column label pair
DataFrame.iat() Access a single value for a row/column pair by integer position
DataFrame.loc() Access a group of rows and columns by label(s)
DataFrame.iloc() Access a group of rows and columns by integer position(s)

4.2. Import and Export

4.2.1. Import data to DataFrame

  • File paths works also with URLs
  • SQL functions uses SQLAlchemy, which supports many RDBMS
pd.read_csv()
pd.read_excel()
pd.read_html()
pd.read_json()
pd.read_sas()
pd.read_sql()        # Read SQL query or database table into a DataFrame
pd.read_sql_query()  # Read SQL query into a DataFrame
pd.read_sql_table()  # Read SQL database table into a DataFrame

4.2.2. Export DataFrame

  • File paths works also with URLs
  • SQL functions uses SQLAlchemy, which supports many RDBMS
DataFrame.to_csv()
DataFrame.to_excel()
DataFrame.to_html()
DataFrame.to_json()
DataFrame.to_latex()
DataFrame.to_dict()
DataFrame.to_sql()

4.3. Display Output

  • Set options for whole script:

    pd.set_option('display.height',1000)
    pd.set_option('display.max_rows',500)
    pd.set_option('display.max_columns',500)
    pd.set_option('display.width',1000)
    
  • Unlimited for whole script:

    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)
    
  • Use config only with context:

    with pd.option_context('display.max_rows', None, 'display.max_columns', 3):
        print(df)
    

4.4. Vizualization

4.4.1. Hist

import matplotlib.pyplot as plt
import pandas as pd


url = 'https://raw.githubusercontent.com/AstroMatt/book-python/master/numerical-analysis/data/iris-clean.csv'
data = pd.read_csv(url)

data.hist()
plt.show()
../_images/matplotlib-pd-hist1.png

Fig. 4.6. Vizualization using hist

4.4.2. Density

import matplotlib.pyplot as plt
import pandas as pd


url = 'https://raw.githubusercontent.com/AstroMatt/book-python/master/numerical-analysis/data/iris-clean.csv'
data = pd.read_csv(url)

data.plot(kind='density', subplots=True, layout=(3,3), sharex=False)
plt.show()
../_images/matplotlib-pd-density1.png

Fig. 4.7. Vizualization using density

4.4.3. Box

import matplotlib.pyplot as plt
import pandas as pd


url = 'https://raw.githubusercontent.com/AstroMatt/book-python/master/numerical-analysis/data/iris-clean.csv'
data = pd.read_csv(url)

data.plot(kind='box', subplots=True, layout=(3,3), sharex=False, sharey=False)
plt.show()
../_images/matplotlib-pd-box1.png

Fig. 4.8. Vizualization using density

4.4.4. Scatter matrix

  • The in pandas version 0.22 plotting module has been moved from pandas.tools.plotting to pandas.plotting
  • As of version 0.19, the pandas.plotting library did not exist
import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import scatter_matrix


url = 'https://raw.githubusercontent.com/AstroMatt/book-python/master/numerical-analysis/data/iris-clean.csv'
data = pd.read_csv(url)

scatter_matrix(data)
plt.show()
../_images/matplotlib-pd-scatter-matrix1.png

Fig. 4.9. Vizualization using density

4.5. Descriptive statistics

Tab. 4.5. Descriptive statistics
Function Description
count Number of non-null observations
sum Sum of values
mean Mean of values
mad Mean absolute deviation
median Arithmetic median of values
min Minimum
max Maximum
mode Mode
abs Absolute Value
prod Product of values
std Unbiased standard deviation
var Unbiased variance
sem Unbiased standard error of the mean
skew Unbiased skewness (3rd moment)
kurt Unbiased kurtosis (4th moment)
quantile Sample quantile (value at %)
cumsum Cumulative sum
cumprod Cumulative product
cummax Cumulative maximum
cummin Cumulative minimum

4.6. Assignments