Skip to main content

Use DataFrame.apply / Series.apply with a default value for Exceptions

Project description

Use DataFrame.apply / Series.apply with a default value for Exceptions

pip install a-pandas-ex-apply-ignore-exceptions
from a_pandas_ex_apply_ignore_exceptions import pd_add_apply_ignore_exceptions

import pandas as pd

pd_add_apply_ignore_exceptions()

df = pd.read_csv(

    "https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv"

)



# the Exception value is the first arg, the rest is just like apply

df1 = df.PassengerId.ds_apply_ignore(pd.NA, lambda x: x / 3)

df2 = df.ds_apply_ignore(pd.NA, lambda x: x["PassengerId"] / 3, axis=1)

df3 = df.PassengerId.ds_apply_ignore(pd.NA, lambda x: x / 0)

df4 = df.ds_apply_ignore(pd.NA, lambda x: x["PassengerId"] / 0, axis=1)

print(df1)

print(df2)

print(df3)

print(df4)





r"""

0        0.333333

1        0.666667

2        1.000000

3        1.333333

4        1.666667

          ...    

886    295.666667

887    296.000000

888    296.333333

889    296.666667

890    297.000000

Name: PassengerId, Length: 891, dtype: float64

0        0.333333

1        0.666667

2        1.000000

3        1.333333

4        1.666667

          ...    

886    295.666667

887    296.000000

888    296.333333

889    296.666667

890    297.000000

Length: 891, dtype: float64

0      <NA>

1      <NA>

2      <NA>

3      <NA>

4      <NA>

       ... 

886    <NA>

887    <NA>

888    <NA>

889    <NA>

890    <NA>

Name: PassengerId, Length: 891, dtype: object

0      <NA>

1      <NA>

2      <NA>

3      <NA>

4      <NA>

       ... 

886    <NA>

887    <NA>

888    <NA>

889    <NA>

890    <NA>

Length: 891, dtype: object



"""



		

Project details


Release history Release notifications | RSS feed

This version

0.10

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 a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz.

File metadata

File hashes

Hashes for a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz
Algorithm Hash digest
SHA256 479eec41c641162a36c0eaf2eb407207c19e3fa1bf9838bc5563ebf0c72f4b37
MD5 e4ae9b0db11ad16367d6138f2e83214f
BLAKE2b-256 bdbc94c54b63fed32719648fa655df6d3893f4f2f7758397eb6c29107080715f

See more details on using hashes here.

File details

Details for the file a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 85fcb1a09efdb3003500d938c34d75a17c4f3bc387e7a7aad5f23bc9cf135017
MD5 696832fe92b77238be2796485a9acf7e
BLAKE2b-256 01b8b797759e264dcc017a0b7bbcf0f67431dbb2d6ab8a137188fa73009b0347

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page