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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz.
File metadata
- Download URL: a_pandas_ex_apply_ignore_exceptions-0.10.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
479eec41c641162a36c0eaf2eb407207c19e3fa1bf9838bc5563ebf0c72f4b37
|
|
| MD5 |
e4ae9b0db11ad16367d6138f2e83214f
|
|
| BLAKE2b-256 |
bdbc94c54b63fed32719648fa655df6d3893f4f2f7758397eb6c29107080715f
|
File details
Details for the file a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl.
File metadata
- Download URL: a_pandas_ex_apply_ignore_exceptions-0.10-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85fcb1a09efdb3003500d938c34d75a17c4f3bc387e7a7aad5f23bc9cf135017
|
|
| MD5 |
696832fe92b77238be2796485a9acf7e
|
|
| BLAKE2b-256 |
01b8b797759e264dcc017a0b7bbcf0f67431dbb2d6ab8a137188fa73009b0347
|