# Apply each value in a column against the whole column
```python
pip install a-pandas-ex-apply-against-all
```
```python
from a_pandas_ex_apply_against_all import pd_add_apply_each
import pandas as pd
pd_add_apply_each()
df = pd.read_csv(
"https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv"
)
df1 = df.PassengerId.s_apply_each(
expression="str(x) + str(y)", # use always x/y in your expression
exception_value=pd.NA,
diagonal_value=pd.NA,
print_exception=True,
)
print(df1)
0 1 2 3 4 ... 886 887 888 889 890
0 <NA> 12 13 14 15 ... 1887 1888 1889 1890 1891
1 21 <NA> 23 24 25 ... 2887 2888 2889 2890 2891
2 31 32 <NA> 34 35 ... 3887 3888 3889 3890 3891
3 41 42 43 <NA> 45 ... 4887 4888 4889 4890 4891
4 51 52 53 54 <NA> ... 5887 5888 5889 5890 5891
.. ... ... ... ... ... ... ... ... ... ... ...
886 8871 8872 8873 8874 8875 ... <NA> 887888 887889 887890 887891
887 8881 8882 8883 8884 8885 ... 888887 <NA> 888889 888890 888891
888 8891 8892 8893 8894 8895 ... 889887 889888 <NA> 889890 889891
889 8901 8902 8903 8904 8905 ... 890887 890888 890889 <NA> 890891
890 8911 8912 8913 8914 8915 ... 891887 891888 891889 891890 <NA>
[891 rows x 891 columns]
# If you use a non-built-in function, you have to pass it as an argument, and use it as "func" in your expression
# An example using shapely (merging different polygons)
from shapely.ops import unary_union
import shapely
polyshape = []
for k in range(10):
xmin = k * 10 + 5
ymin = k * 10 + 5
xmax = k * 20 + 10
ymax = k * 20 + 10
coordsalls = [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax], [xmin, ymin]]
po = shapely.geometry.Polygon(coordsalls)
polyshape.append(po)
df2 = pd.DataFrame(polyshape)
df2
Out[7]:
0
0 POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))
1 POLYGON ((15 15, 30 15, 30 30, 15 30, 15 15))
2 POLYGON ((25 25, 50 25, 50 50, 25 50, 25 25))
3 POLYGON ((35 35, 70 35, 70 70, 35 70, 35 35))
4 POLYGON ((45 45, 90 45, 90 90, 45 90, 45 45))
5 POLYGON ((55 55, 110 55, 110 110, 55 110, 55 55))
6 POLYGON ((65 65, 130 65, 130 130, 65 130, 65 65))
7 POLYGON ((75 75, 150 75, 150 150, 75 150, 75 75))
8 POLYGON ((85 85, 170 85, 170 170, 85 170, 85 85))
9 POLYGON ((95 95, 190 95, 190 190, 95 190, 95 95))
df1 = df2[0].s_apply_each(
expression="func([x,y]) if x.intersects(y) else x", # use always x/y in your expression
func=unary_union,
exception_value=pd.NA,
diagonal_value=pd.NA,
print_exception=True,
ignore_exceptions=True
)
Out[8]:
0 ... 9
0 <NA> ... POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))
1 POLYGON ((15 15, 30 15, 30 30, 15 30, 15 15)) ... POLYGON ((15 15, 30 15, 30 30, 15 30, 15 15))
2 POLYGON ((25 25, 50 25, 50 50, 25 50, 25 25)) ... POLYGON ((25 25, 50 25, 50 50, 25 50, 25 25))
3 POLYGON ((35 35, 70 35, 70 70, 35 70, 35 35)) ... POLYGON ((35 35, 70 35, 70 70, 35 70, 35 35))
4 POLYGON ((45 45, 90 45, 90 90, 45 90, 45 45)) ... POLYGON ((45 45, 90 45, 90 90, 45 90, 45 45))
5 POLYGON ((55 55, 110 55, 110 110, 55 110, 55 55)) ... POLYGON ((110 55, 55 55, 55 110, 95 110, 95 19...
6 POLYGON ((65 65, 130 65, 130 130, 65 130, 65 65)) ... POLYGON ((130 65, 65 65, 65 130, 95 130, 95 19...
7 POLYGON ((75 75, 150 75, 150 150, 75 150, 75 75)) ... POLYGON ((150 75, 75 75, 75 150, 95 150, 95 19...
8 POLYGON ((85 85, 170 85, 170 170, 85 170, 85 85)) ... POLYGON ((170 85, 85 85, 85 170, 95 170, 95 19...
9 POLYGON ((95 95, 190 95, 190 190, 95 190, 95 95)) ... <NA>
[10 rows x 10 columns]
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/a_pandas_ex_apply_against_all",
"name": "a-pandas-ex-apply-against-all",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "pandas,DataFrame,apply",
"author": "Johannes Fischer",
"author_email": "<aulasparticularesdealemaosp@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/da/48/e79742f53c401daf82902884f5a3bbd22fb6bf35943031e7ebcdb4f6e285/a_pandas_ex_apply_against_all-0.12.tar.gz",
"platform": null,
"description": "\n# Apply each value in a column against the whole column\n\n\n\n```python\n\npip install a-pandas-ex-apply-against-all\n\n```\n\n\n\n```python\n\n\n\nfrom a_pandas_ex_apply_against_all import pd_add_apply_each\n\nimport pandas as pd\n\npd_add_apply_each()\n\ndf = pd.read_csv(\n\n \"https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv\"\n\n)\n\ndf1 = df.PassengerId.s_apply_each(\n\n expression=\"str(x) + str(y)\", # use always x/y in your expression\n\n exception_value=pd.NA,\n\n diagonal_value=pd.NA,\n\n print_exception=True,\n\n)\n\nprint(df1)\n\n 0 1 2 3 4 ... 886 887 888 889 890\n\n0 <NA> 12 13 14 15 ... 1887 1888 1889 1890 1891\n\n1 21 <NA> 23 24 25 ... 2887 2888 2889 2890 2891\n\n2 31 32 <NA> 34 35 ... 3887 3888 3889 3890 3891\n\n3 41 42 43 <NA> 45 ... 4887 4888 4889 4890 4891\n\n4 51 52 53 54 <NA> ... 5887 5888 5889 5890 5891\n\n.. ... ... ... ... ... ... ... ... ... ... ...\n\n886 8871 8872 8873 8874 8875 ... <NA> 887888 887889 887890 887891\n\n887 8881 8882 8883 8884 8885 ... 888887 <NA> 888889 888890 888891\n\n888 8891 8892 8893 8894 8895 ... 889887 889888 <NA> 889890 889891\n\n889 8901 8902 8903 8904 8905 ... 890887 890888 890889 <NA> 890891\n\n890 8911 8912 8913 8914 8915 ... 891887 891888 891889 891890 <NA>\n\n[891 rows x 891 columns]\n\n\n\n# If you use a non-built-in function, you have to pass it as an argument, and use it as \"func\" in your expression\n\n# An example using shapely (merging different polygons)\n\n\n\nfrom shapely.ops import unary_union\n\nimport shapely\n\npolyshape = []\n\nfor k in range(10):\n\n xmin = k * 10 + 5\n\n ymin = k * 10 + 5\n\n xmax = k * 20 + 10\n\n ymax = k * 20 + 10\n\n coordsalls = [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax], [xmin, ymin]]\n\n po = shapely.geometry.Polygon(coordsalls)\n\n polyshape.append(po)\n\ndf2 = pd.DataFrame(polyshape)\n\n\n\ndf2\n\nOut[7]: \n\n 0\n\n0 POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))\n\n1 POLYGON ((15 15, 30 15, 30 30, 15 30, 15 15))\n\n2 POLYGON ((25 25, 50 25, 50 50, 25 50, 25 25))\n\n3 POLYGON ((35 35, 70 35, 70 70, 35 70, 35 35))\n\n4 POLYGON ((45 45, 90 45, 90 90, 45 90, 45 45))\n\n5 POLYGON ((55 55, 110 55, 110 110, 55 110, 55 55))\n\n6 POLYGON ((65 65, 130 65, 130 130, 65 130, 65 65))\n\n7 POLYGON ((75 75, 150 75, 150 150, 75 150, 75 75))\n\n8 POLYGON ((85 85, 170 85, 170 170, 85 170, 85 85))\n\n9 POLYGON ((95 95, 190 95, 190 190, 95 190, 95 95))\n\n\n\n\n\ndf1 = df2[0].s_apply_each(\n\n expression=\"func([x,y]) if x.intersects(y) else x\", # use always x/y in your expression\n\n func=unary_union,\n\n exception_value=pd.NA,\n\n diagonal_value=pd.NA,\n\n print_exception=True,\n\n ignore_exceptions=True\n\n)\n\n\n\nOut[8]: \n\n 0 ... 9\n\n0 <NA> ... POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))\n\n1 POLYGON ((15 15, 30 15, 30 30, 15 30, 15 15)) ... POLYGON ((15 15, 30 15, 30 30, 15 30, 15 15))\n\n2 POLYGON ((25 25, 50 25, 50 50, 25 50, 25 25)) ... POLYGON ((25 25, 50 25, 50 50, 25 50, 25 25))\n\n3 POLYGON ((35 35, 70 35, 70 70, 35 70, 35 35)) ... POLYGON ((35 35, 70 35, 70 70, 35 70, 35 35))\n\n4 POLYGON ((45 45, 90 45, 90 90, 45 90, 45 45)) ... POLYGON ((45 45, 90 45, 90 90, 45 90, 45 45))\n\n5 POLYGON ((55 55, 110 55, 110 110, 55 110, 55 55)) ... POLYGON ((110 55, 55 55, 55 110, 95 110, 95 19...\n\n6 POLYGON ((65 65, 130 65, 130 130, 65 130, 65 65)) ... POLYGON ((130 65, 65 65, 65 130, 95 130, 95 19...\n\n7 POLYGON ((75 75, 150 75, 150 150, 75 150, 75 75)) ... POLYGON ((150 75, 75 75, 75 150, 95 150, 95 19...\n\n8 POLYGON ((85 85, 170 85, 170 170, 85 170, 85 85)) ... POLYGON ((170 85, 85 85, 85 170, 95 170, 95 19...\n\n9 POLYGON ((95 95, 190 95, 190 190, 95 190, 95 95)) ... <NA>\n\n[10 rows x 10 columns]\n\n\n\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Apply each value in a column against the whole column",
"version": "0.12",
"split_keywords": [
"pandas",
"dataframe",
"apply"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "d28d6302540fa36056f5d43f4272487b",
"sha256": "cfb2f321952a6a7d3df1b980b215cbc059fddd5b1eab8cebf9c34decde82b6b4"
},
"downloads": -1,
"filename": "a_pandas_ex_apply_against_all-0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d28d6302540fa36056f5d43f4272487b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7326,
"upload_time": "2022-12-19T16:14:11",
"upload_time_iso_8601": "2022-12-19T16:14:11.904104Z",
"url": "https://files.pythonhosted.org/packages/99/4e/1ba3d80836ef13200ea08f3827a11a52a9df1cb2c217634e6c7fd5578a9a/a_pandas_ex_apply_against_all-0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "96dfd9841e38ac0659693f8905894023",
"sha256": "6af813a4084d8df1e1e7c5cafa36caec92d92a42cedec87385d710321b61e023"
},
"downloads": -1,
"filename": "a_pandas_ex_apply_against_all-0.12.tar.gz",
"has_sig": false,
"md5_digest": "96dfd9841e38ac0659693f8905894023",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5638,
"upload_time": "2022-12-19T16:14:13",
"upload_time_iso_8601": "2022-12-19T16:14:13.372843Z",
"url": "https://files.pythonhosted.org/packages/da/48/e79742f53c401daf82902884f5a3bbd22fb6bf35943031e7ebcdb4f6e285/a_pandas_ex_apply_against_all-0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-19 16:14:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "a_pandas_ex_apply_against_all",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "pandas",
"specs": []
}
],
"lcname": "a-pandas-ex-apply-against-all"
}