Skip to main content

Helper functions from the Pandas Illustrated guide

Project description

п»ї# pandas-illustrated

pypi python pytest Coverage Badge Code style: black License

This repo contains code for a number of helper functions mentioned in the Pandas Illustrated guide.

Installation:

pip install pandas-illustrated

Contents

Basic operations:

  • find(s, x, pos=False)
  • findall(s, x, pos=False)
  • insert(dst, pos, value, label, axis=0, ignore_index = False, order=None, allow_duplicates=False, inplace=False)
  • append(dst, value, label = lib.no_default, axis=0, ignore_index = False, order=None, allow_duplicates: bool = False, inplace=False)
  • drop(obj, items=None, like=None, regex=None, axis=None)
  • move(obj, pos, label=None, column=None, index=None, axis=None, reset_index=False)
  • join(dfs, on=None, how="left", suffixes=None)

Visualization improvements:

  • patch_series_repr(footer=True)
  • unpatch_series_repr()
  • sidebyside(*dfs, names=[], index=True, valign="top")
  • sbs = sidebyside

MultiIndex helpers:

  • patch_mi_co()
  • from_dict(d)
  • from_kw(**kwargs)

Locking columns order:

  • locked(obj, level=None, axis=None, categories=None, inplace=False)
  • lock = locked with inplace=True
  • vis_lock(obj, checkmark="вњ“")
  • vis_patch()
  • vis_unpatch()
  • from_product(iterables, sortorder=None, names=lib.no_default, lock=True)

MultiIndex manipulations:

  • get_level(obj, level_id, axis=None)
  • set_level(obj, level_id, labels, name=lib.no_default, axis=None, inplace=False)
  • move_level(obj, src, dst, axis=None, inplace=False, sort=False)
  • insert_level(obj, pos, labels, name=lib.no_default, axis=None, inplace=False, sort=False)
  • drop_level(obj, level_id, axis=None, inplace=False)
  • swap_levels(obj, i: Axis = -2, j: Axis = -1, axis: Axis = None, inplace=False, sort=False)
  • join_levels(obj, name=None, sep="_", axis=None, inplace=False)
  • split_level(obj, names=None, sep="_", axis=None, inplace=False)
  • rename_level(obj, mapping, level_id=None, axis=None, inplace=False)

Usage

find and findall

By default find(series, value) looks for the first occurrence of the given value in a series and returns the corresponsing index label.

>>> import pandas as pd
>>> import pdi

>>> s = pd.Series([4, 2, 4, 6], index=['cat', 'penguin', 'dog', 'butterfly'])

>>> pdi.find(s, 2)
'penguin' 

>>> pdi.find(s, 4)
'cat' 

When the value is not found raises a ValueError.

findall(series, value) returns a (possibly empty) index of all matching occurrences:

>>> pdi.findall(s, 4)
Index(['cat', 'dog'], dtype='object')

With pos=True keyword argument find() and findall() return the positional index instead:

>>> pdi.find(s, 2, pos=True)
1 

>>> pdi.find(s, 4, pos=True)
0

There is a number of ways to find index label for a given value. The most efficient of them are:

†s.index[s.tolist().index(x)]       # faster for Series with less than 1000 elements
†s.index[np.where(s == x)[0][0]]    # faster for Series with over 1000 elements  

find() chooses optimal implementation depending on the series size; findall() always uses the where implementation.

Improving Series Representation

Run pdi.patch_series_repr() to make Series look better:

If you want to display several Series from one cell, call display(s) for each.

Displaying several Pandas objects side vy side

To display several dataframes, series or indices side by side run pdi.sidebyside(s1, s2, ...)

Testing

Run pytest in the project root.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandas-illustrated-0.5.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

pandas_illustrated-0.5-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file pandas-illustrated-0.5.tar.gz.

File metadata

  • Download URL: pandas-illustrated-0.5.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.1

File hashes

Hashes for pandas-illustrated-0.5.tar.gz
Algorithm Hash digest
SHA256 1f592480585d2774c21ddbea82df92acdb8f1a980364d9308678a9212f2c7a98
MD5 890814a36e3506abe0e71f063622dbd5
BLAKE2b-256 3016b525a5a3e0f902b431341023b0df3508b9448fec2eb752afddbc8de0b891

See more details on using hashes here.

Provenance

File details

Details for the file pandas_illustrated-0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for pandas_illustrated-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6259afb752450ae6a05c0a1297fe004d9eeac862abee76da0b6eebfb22ca2853
MD5 0c794e73da8097328f1b7d30f0ea830b
BLAKE2b-256 0be88f4046c17122eed0cd3cf73ea962dd3e629f76041dda3fe828fe68367485

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page