Skip to main content

pandacell

Author: Eirik B. Stavestrand

Introduces a %df (or %%df) magic which can be used in Jupyter notebooks and the IPython console.

The magic executes the contents of a cell on a Pandas DataFrame.

Description

Pandas is great and all, but writing Pandas code can be tedious. For example when simply making summing two columns:

    In [1]: df["a"] + df["b"]

It might not look like such a big deal, but all those brackets and quotation marks add up. Using pandacell, the above syntax can be written as:

    In [2]: %df a + b

Under the hoods, this is accomplished simply by passing the cell contents as a string to Pandas' df.eval function. This isn't very complex, but it does provide a fair deal of functionality and adds a whole lot of readability.

If you wish to store the results to a new column, use regular assignment along with the -i (or --inplace) flag:

    In [3]: %df -i c = a + b

It also works with multiple assignments:

    In [4]: %%df -i
       ...: c = a + b
       ...: f = c - a

You can use Pandas' various accessors and series method calls:

    In [5]: %%df -i
       ...: name_upper = name.str.upper()
       ...: yr = timestamp.dt.year
       ...: lower_cased = species.where(cond=species.str[0].str.islower(), other=None)

Since variable names are assumed to be columns in the dataframe, regular variables in the local/global namespace can be accessed by prefixing with @

    In [6]: a = 1
       ...: %df a = @a + 1

    In [7]: def myfunc(row):
        ...:     return row + 43
        ...: %df b = a.apply(@myfunc)

By default, pandacell operates on any dataframe named df. This can be overridden with the -n (or --name) flag:

    In [8]: %df -n=df_in c = a + b

You can also print subset a dataframe with the -q (or --query) flag:

    In [9]: %df -q species == "setosa"
    Out[9]:
        sepal_length  sepal_width  petal_length  petal_width species  a
    0            5.1          3.5           1.4          0.2  setosa  0
    1            4.9          3.0           1.4          0.2  setosa  0


    In [10]: %df -q species.isna() #check for missing values
    Out[10]:
    Empty DataFrame
    Columns: [sepal_length, sepal_width, petal_length, petal_width, species]
    Index: []

This can be combined with the -i flag to subset the dataframe in-place:

    In [10]: %df -q -i species == "setosa"

Pandacell even supports comments

    In [11]: %%df -i
       ...: # Line comment
       ...: c = a + b # Comment at end of line

Inspired by

https://github.com/catherinedevlin/ipython-sql

Development

https://github.com/eirki/pandacell

Release files for pandacell 2020.9.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pandacell 2020.9.19
File Size Uploaded
pandacell-2020.9.19.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pandacell 2020.9.19
File Interpreter ABI Platform
pandacell-2020.9.19-py3-none-any.whl Python 3 none any Details

Total release size: 6.8 kB

Release files / pandacell-2020.9.19.tar.gz

Download URL pandacell-2020.9.19.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6ac93b932fce46e73d0b14d8546d252fa5fb91e00f6f7634b8bbe591ba9b1fe9
BLAKE2b-256 checksum
How to use checksums
b65615de0418333fd99ed341228e3362c22dd7ef922fddad0b5fa4ea472c2868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

Release files / pandacell-2020.9.19-py3-none-any.whl

Download URL pandacell-2020.9.19-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffe0a9beeb42d7c741125300b69ffe4c1d1bac998914d60d279ec3c51e52b7cf
BLAKE2b-256 checksum
How to use checksums
7f7cccdaa697eb16ce149e4d8d46c04df9e7d0b8591fa246d2a6ef60c50285fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

2020.9.19 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page