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
  • findall
  • insert
  • drop
  • move
  • join

Visualization improvements:

  • patch_series_repr
  • unpatch_series_repr
  • sidebyside
  • sbs

MultiIndex helpers:

  • patch_mi_co
  • from_dict
  • from_kw

Locking columns order:

  • locked
  • lock
  • vis_lock
  • vis_patch
  • vis_unpatch
  • from_product

Level manipulations:

  • get_level
  • set_level
  • move_level
  • insert_level
  • drop_level
  • swap_levels
  • join_levels
  • split_level
  • rename_level

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.3.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

pandas_illustrated-0.3-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pandas-illustrated-0.3.tar.gz
  • Upload date:
  • Size: 16.0 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.3.tar.gz
Algorithm Hash digest
SHA256 40a59edcaa9fa888466034701b224f37d080aa34e417a6a1e93175fc7ce7d9ce
MD5 d16d339eaba9f2119557c4e88e36048b
BLAKE2b-256 1c8c8a80275402c53477a329261fd034b2e60beab41f31d458cac9b1f6541b93

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pandas_illustrated-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a6310807e20b0c5b79f3ad77778d11da5a83c8317f7518ceb0f6e0abe7139345
MD5 751d20b9d88e2f05711b143cc9eae8fd
BLAKE2b-256 50894599591120f903408da8d91a549bd9a666fecaefff5b1c60d13d7715248d

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