Functor to convert string boolean expressions into Pandas DataFrame filters.
Project description
apply-pandas
Generic Apply Function for Pandas DataFrames
This repo demonstrates a powerful, simple and generic way to make an apply
function for Pandas DataFrames. apply
takes in a list of conditions (as strings) to apply to a dataframe, and returns the result of those applications.
This can be useful when you want to avoid writing huge chunks of code to filter data, or when you want to construct code based on other factors to give you a properly formatted result. Case in point - user input can be turned into well-defined string queries/filters on your DataFrame, with which you can return the appropriate data, without knowing those queries in advance.
Overview and Usage
The Apply function is wrapped in a functor so that we can cleanly separate verification and application. Furthermore, this also allows us to clearly bind the target DatFrame and the desired conditions together; something that might not always be easy when dealing with rapidly changing variables, while giving the programmer the option to call it when desired.
In effect, you can bind at creation, but can safely get the computation much, much later. Here's a code snippet that illustrates the usage of this method.
# First, we create our dummy data
df = pd.DataFrame(list(range(0, 100)), columns=["Slice"])
# Then a list of conditions to apply.
list_condns = ["frame['Slice']<5", "frame['Slice']%3==1"]
# To use the Apply() class,
# First create an object that binds DataFrame and conditions,
Applicator = Apply(df, list_condns)
# Call for the result when needed.
result = Applicator() #<--- returns pandas DataFrame
Notice that in list_condns
, all of our conditions are made out to frame
, regardless of the actual name of the actual target frame. This means that when you generate these conditions in real-time, you do not ever have to worry about getting the name of the variable - just make out the filter for frame
, and we'll take care of the rest :)
Bugfixes and Improvements
This code is always ready for improvements, and to get rid of bugs - so please let me know if you've found anything, or how you've used this project! Hope you have fun with it :)
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file apply-pandas-0.0.1.tar.gz
.
File metadata
- Download URL: apply-pandas-0.0.1.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1af3f08ac7f79cabe6e6c1ba685f94689dcf7d571c4543f3634dedf47a727e57 |
|
MD5 | 867e2b05574ce63a8bf51ba5dd5e31c8 |
|
BLAKE2b-256 | a69d8189c328e336660975190ee264d8f36dd07d44a2c305aedb0cc4001f7d84 |
File details
Details for the file apply_pandas-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: apply_pandas-0.0.1-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 663fb92387d23e2f4aa1c75b28c7ef21438ca83fdc95a6b037a08dca21d13335 |
|
MD5 | 405d373c1f71ba4217ace97843bc7c23 |
|
BLAKE2b-256 | 128c17ed5ff9cab9945ae8d94cd09fe4b59eabd3d5ed752e6031d0fb0fba0646 |