Helper functions from the Pandas Illustrated Guide
Project description
п»ї# pandas-illustrated
This repo contains code for find()
and findall()
functions that help in searching Series by value.
Chooses optimal implementation depending on the size of the Series.
See Pandas Illustrated article for details.
Installation:
pip install pandas-illustrated
Usage
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
Performance
There's 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()
to make Series look better:
If you want to display several Series from one cell, call display(s)
for each.
To display several Series side by side call pdi.sidebyside(s1, s2, ...)
Testing
Run pytest
in 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
Built Distribution
Hashes for pandas_illustrated-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78ad3bc124a2160e002967519ead3f4fac1a31e90e03e32bd8f766bba4566b7c |
|
MD5 | 26709b420eac1b0e02be57aa1e16e4de |
|
BLAKE2b-256 | d5e21e2983d6de5f40362afb9991cde7c8eabf2088ff69308d374243c3ef2be5 |