a-pandas-ex-column-reduce


Namea-pandas-ex-column-reduce JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/a_pandas_ex_column_reduce
SummaryApply reduce against a whole Pandas Series
upload_time2022-12-19 16:32:12
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords pandas dataframe reduce series apply
VCS
bugtrack_url
requirements pandas
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Apply reduce against a whole Pandas Series



```python

pip install a-pandas-ex-column-reduce

```



```python



from a_pandas_ex_column_reduce import pd_add_column_reduce

import pandas as pd

pd_add_column_reduce()

df = pd.read_csv(

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

)

df = df[:10]

result1 = df.PassengerId.s_column_reduce(

    expression="str(x) +  str(y)",  # the expression has to be passed as a string and must contain x/y

    own_value_against_own_value=True,  # if False: skips when "index of x == index of y"

    ignore_exceptions=True,  # will ignore the execution of the expression and will go on

    print_exceptions=True,

)

print(f"\n\n{result1=}")



result1=0     112345678910

1     212345678910

2     312345678910

3     412345678910

4     512345678910

5     612345678910

6     712345678910

7     812345678910

8     912345678910

9    1012345678910

dtype: object





result2 = df.PassengerId.s_column_reduce(

    expression="x + y",

    own_value_against_own_value=True,

    ignore_exceptions=True,

    print_exceptions=True,

)

print(f"\n\n{result2=}")



result2=0    56

1    57

2    58

3    59

4    60

5    61

6    62

7    63

8    64

9    65

dtype: int64





# Updates the column after each iteration

result3 = df.PassengerId.s_column_reduce_update(

    expression="x + y if y > 5 else x",

    own_value_against_own_value=True,

    ignore_exceptions=True,

    print_exceptions=True,

)





print(f"\n\n{result3=}")





result3=0       41

1       83

2      167

3      335

4      671

5     1343

6     2681

7     5356

8    10705

9    21402

Name: PassengerId, dtype: int64





# If you use a non-built-in function, you have to pass the function 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)

print(f"\n\n{df2=}")

dfj = df2[0].s_column_reduce(

    expression="func([x,y]) if x.intersects(y) else x",

    func=unary_union,

    own_value_against_own_value=True,

    ignore_exceptions=True,

)

print(f"\n\n{dfj=}")

dfj2 = df2[0].s_column_reduce_update(

    expression="func([x,y]) if x.intersects(y) else x",

    func=unary_union,

    own_value_against_own_value=False,

    ignore_exceptions=True,

)

print(f"\n\n{dfj2=}")





df2=                                                   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))



dfj=0              POLYGON ((5 5, 5 10, 10 10, 10 5, 5 5))

1    POLYGON ((55 90, 55 110, 65 110, 65 130, 75 13...

2    POLYGON ((45 90, 55 90, 55 110, 65 110, 65 130...

3    POLYGON ((55 90, 55 110, 65 110, 65 130, 75 13...

4    POLYGON ((35 70, 45 70, 45 90, 55 90, 55 110, ...

5    POLYGON ((45 70, 45 90, 55 90, 55 110, 65 110,...

6    POLYGON ((45 70, 45 90, 55 90, 55 110, 65 110,...

7    POLYGON ((45 90, 55 90, 55 110, 65 110, 65 130...

8    POLYGON ((90 55, 90 45, 45 45, 45 90, 55 90, 5...

9    POLYGON ((130 65, 110 65, 110 55, 55 55, 55 11...

dtype: object



dfj2=0              POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))

1    POLYGON ((45 70, 45 90, 55 90, 55 110, 65 110,...

2    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...

3    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...

4    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...

5    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...

6    POLYGON ((50 25, 30 25, 30 15, 15 15, 15 30, 2...

7    POLYGON ((85 150, 85 170, 95 170, 95 190, 190 ...

8    POLYGON ((85 150, 85 170, 95 170, 95 190, 190 ...

9    POLYGON ((85 150, 85 170, 95 170, 95 190, 190 ...

Name: 0, dtype: object

```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/a_pandas_ex_column_reduce",
    "name": "a-pandas-ex-column-reduce",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "pandas,DataFrame,reduce,Series,apply",
    "author": "Johannes Fischer",
    "author_email": "<aulasparticularesdealemaosp@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b1/15/2719085482afdc2ea126d7f8288b3917f5304b4a5026bac5a76066a57d45/a_pandas_ex_column_reduce-0.10.tar.gz",
    "platform": null,
    "description": "\n# Apply reduce against a whole Pandas Series\n\n\n\n```python\n\npip install a-pandas-ex-column-reduce\n\n```\n\n\n\n```python\n\n\n\nfrom a_pandas_ex_column_reduce import pd_add_column_reduce\n\nimport pandas as pd\n\npd_add_column_reduce()\n\ndf = pd.read_csv(\n\n    \"https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv\"\n\n)\n\ndf = df[:10]\n\nresult1 = df.PassengerId.s_column_reduce(\n\n    expression=\"str(x) +  str(y)\",  # the expression has to be passed as a string and must contain x/y\n\n    own_value_against_own_value=True,  # if False: skips when \"index of x == index of y\"\n\n    ignore_exceptions=True,  # will ignore the execution of the expression and will go on\n\n    print_exceptions=True,\n\n)\n\nprint(f\"\\n\\n{result1=}\")\n\n\n\nresult1=0     112345678910\n\n1     212345678910\n\n2     312345678910\n\n3     412345678910\n\n4     512345678910\n\n5     612345678910\n\n6     712345678910\n\n7     812345678910\n\n8     912345678910\n\n9    1012345678910\n\ndtype: object\n\n\n\n\n\nresult2 = df.PassengerId.s_column_reduce(\n\n    expression=\"x + y\",\n\n    own_value_against_own_value=True,\n\n    ignore_exceptions=True,\n\n    print_exceptions=True,\n\n)\n\nprint(f\"\\n\\n{result2=}\")\n\n\n\nresult2=0    56\n\n1    57\n\n2    58\n\n3    59\n\n4    60\n\n5    61\n\n6    62\n\n7    63\n\n8    64\n\n9    65\n\ndtype: int64\n\n\n\n\n\n# Updates the column after each iteration\n\nresult3 = df.PassengerId.s_column_reduce_update(\n\n    expression=\"x + y if y > 5 else x\",\n\n    own_value_against_own_value=True,\n\n    ignore_exceptions=True,\n\n    print_exceptions=True,\n\n)\n\n\n\n\n\nprint(f\"\\n\\n{result3=}\")\n\n\n\n\n\nresult3=0       41\n\n1       83\n\n2      167\n\n3      335\n\n4      671\n\n5     1343\n\n6     2681\n\n7     5356\n\n8    10705\n\n9    21402\n\nName: PassengerId, dtype: int64\n\n\n\n\n\n# If you use a non-built-in function, you have to pass the function as an argument, and use it as \"func\" in your expression\n\n# An example using shapely (merging different polygons)\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\n\t\n\ndf2 = pd.DataFrame(polyshape)\n\nprint(f\"\\n\\n{df2=}\")\n\ndfj = df2[0].s_column_reduce(\n\n    expression=\"func([x,y]) if x.intersects(y) else x\",\n\n    func=unary_union,\n\n    own_value_against_own_value=True,\n\n    ignore_exceptions=True,\n\n)\n\nprint(f\"\\n\\n{dfj=}\")\n\ndfj2 = df2[0].s_column_reduce_update(\n\n    expression=\"func([x,y]) if x.intersects(y) else x\",\n\n    func=unary_union,\n\n    own_value_against_own_value=False,\n\n    ignore_exceptions=True,\n\n)\n\nprint(f\"\\n\\n{dfj2=}\")\n\n\n\n\n\ndf2=                                                   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\ndfj=0              POLYGON ((5 5, 5 10, 10 10, 10 5, 5 5))\n\n1    POLYGON ((55 90, 55 110, 65 110, 65 130, 75 13...\n\n2    POLYGON ((45 90, 55 90, 55 110, 65 110, 65 130...\n\n3    POLYGON ((55 90, 55 110, 65 110, 65 130, 75 13...\n\n4    POLYGON ((35 70, 45 70, 45 90, 55 90, 55 110, ...\n\n5    POLYGON ((45 70, 45 90, 55 90, 55 110, 65 110,...\n\n6    POLYGON ((45 70, 45 90, 55 90, 55 110, 65 110,...\n\n7    POLYGON ((45 90, 55 90, 55 110, 65 110, 65 130...\n\n8    POLYGON ((90 55, 90 45, 45 45, 45 90, 55 90, 5...\n\n9    POLYGON ((130 65, 110 65, 110 55, 55 55, 55 11...\n\ndtype: object\n\n\n\ndfj2=0              POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))\n\n1    POLYGON ((45 70, 45 90, 55 90, 55 110, 65 110,...\n\n2    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...\n\n3    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...\n\n4    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...\n\n5    POLYGON ((90 45, 70 45, 70 35, 50 35, 50 25, 3...\n\n6    POLYGON ((50 25, 30 25, 30 15, 15 15, 15 30, 2...\n\n7    POLYGON ((85 150, 85 170, 95 170, 95 190, 190 ...\n\n8    POLYGON ((85 150, 85 170, 95 170, 95 190, 190 ...\n\n9    POLYGON ((85 150, 85 170, 95 170, 95 190, 190 ...\n\nName: 0, dtype: object\n\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Apply reduce against a whole Pandas Series",
    "version": "0.10",
    "split_keywords": [
        "pandas",
        "dataframe",
        "reduce",
        "series",
        "apply"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "1b02916c6d10b396debb14421df62c82",
                "sha256": "2553cb7a33ad5a258300cae8d722ae4583cdb203f70b6aa3352a8125760bb091"
            },
            "downloads": -1,
            "filename": "a_pandas_ex_column_reduce-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1b02916c6d10b396debb14421df62c82",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7399,
            "upload_time": "2022-12-19T16:32:10",
            "upload_time_iso_8601": "2022-12-19T16:32:10.419068Z",
            "url": "https://files.pythonhosted.org/packages/08/db/3a495ce5bec53acd2f56e236909c9cb80fe67a4279711fc61acd18ccf6cb/a_pandas_ex_column_reduce-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "aed33315c6e5a6ad844d7613fe363aef",
                "sha256": "7d4cf1b9fe1b8bb933dd209c34fcb4f15d2a23f3e1398a2a3b74bac18afcd17b"
            },
            "downloads": -1,
            "filename": "a_pandas_ex_column_reduce-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "aed33315c6e5a6ad844d7613fe363aef",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5964,
            "upload_time": "2022-12-19T16:32:12",
            "upload_time_iso_8601": "2022-12-19T16:32:12.151447Z",
            "url": "https://files.pythonhosted.org/packages/b1/15/2719085482afdc2ea126d7f8288b3917f5304b4a5026bac5a76066a57d45/a_pandas_ex_column_reduce-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-19 16:32:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hansalemaos",
    "github_project": "a_pandas_ex_column_reduce",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pandas",
            "specs": []
        }
    ],
    "lcname": "a-pandas-ex-column-reduce"
}
        
Elapsed time: 0.03243s