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
"""
Metadata
Release files for a-pandas-ex-apply-ignore-exceptions 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz | 3.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.3 kB
Release files / a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz
| Download URL | a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
479eec41c641162a36c0eaf2eb407207c19e3fa1bf9838bc5563ebf0c72f4b37
|
|
BLAKE2b-256 checksum How to use checksums |
bdbc94c54b63fed32719648fa655df6d3893f4f2f7758397eb6c29107080715f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl
| Download URL | a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85fcb1a09efdb3003500d938c34d75a17c4f3bc387e7a7aad5f23bc9cf135017
|
|
BLAKE2b-256 checksum How to use checksums |
01b8b797759e264dcc017a0b7bbcf0f67431dbb2d6ab8a137188fa73009b0347
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|