Skip to main content

Split a dataframe by boolean array

Project description



pandas-refract: Convenient partitioning by Truthy/Falsey array
===============================================

<b>pandas-refract<b> is an MIT licensed Python package with a simple function that
allows users to divide their dataframes by the 'Truthy' and 'Falseyness' of a provided array.



Eventually, the goal of this package is an additional feature to the Pandas library that allows users to .pop rows
from a dataframe where a condition is met. As far as I can tell this is not possible like the below example.

Ideal case would be:

target_df = df.pop(df['target_column'] == 'target_value')
non_target_df = df

What is required now is:

target_df = df[df['target_column'] == 'target_value']
non_target_df = df[df['target_column'] != 'targe_value']


Obviously, this package is not providing anything not currently possible in the current Pandas library. It does,
however, add a layer of convenience for more complex slicing where you need to separate, not remove, rows by conditions.


Examples
========

Simplest example of current Pandas requires:

df1 = df[df.column.notnull()].reset_index(drop=True)
df2 = df[df.column.isnull()].reset_index(drop=True)

or

df1 = df[df.column == 'test_string'].reset_index(drop=True)
df2 = df[df.column != 'test_string'].reset_index(drop=True)


With pandas-refract this becomes:

df1, df2 = refract(df, df.column.notnull(), True]

and

df1, df2 = refract(df, df.column == test_string', True]


But you don't have to pass it explicit boolean arrays:

data = {'a': ['', 'truthy', '', 'truthy'],
'b': [0, 1, 2, 3]
}

df = pd.DataFrame(data)

truthy_df, falsey_df = refract(df, df.a)


More complex examples:
<br>
*(where 'a' is Falsey and 'b' is an odd number):*

df1, df2 = refract(df, ((~df.a) & (df.b % 2 == 1)))



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_refract-1.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

pandas_refract-1-py2.py3-none-any.whl (2.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pandas_refract-1.tar.gz.

File metadata

  • Download URL: pandas_refract-1.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pandas_refract-1.tar.gz
Algorithm Hash digest
SHA256 06f56494eaea9f6bbe12881d47a568feb18417e0f4d7e1809b56d2fedf9cb5df
MD5 19e42d3f4264d560f2f5e123f0189334
BLAKE2b-256 8ea10e9af3f9ea1aba3887d4d44a8395755eb34d5e88216581a5e2a65a8d6bc1

See more details on using hashes here.

File details

Details for the file pandas_refract-1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pandas_refract-1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f6e87ae31aa42684e649cd8bcbcc8d5a061fb8e8bbe5b9dd72998d6cf20f6a08
MD5 3b10cf6a10f6d2cd9b6bbccfea438cf9
BLAKE2b-256 075e59811b4b869e0a8d3a7c77fdeb95aeef58a650e8dea69a198ca3f180d97f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page