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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pandas-illustrated-0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 d1aa02f1a528849c0b7bd10f9442dc6dc4ac2ec8a8161a40abcb76fd4bad4d51
MD5 2fba9f27e2816d0aee976bc140f9b5b5
BLAKE2b-256 bc85f8406fc70c80ebd7e616564cc5ed60219a4e67c7d8936a42063e9f8640c1

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pandas_illustrated-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 90c7ef40d1e204c35f92d77343834296ee7388e26eb241e832d50647755fc9cc
MD5 60ed4acfad352647564cab8365c97f2b
BLAKE2b-256 5adaef25ce1fe32c2c3375719b86c4d80026b42a31627016de3c3efd0b1a5d21

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