Package: a-pandas-ex-column-reduce
Apply reduce against a whole Pandas Seriespipinstalla-pandas-ex-column-reducefroma_pandas_ex_column_reduceimportpd_add_column_reduceimportpandasaspdpd_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/yown_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 onprint_exceptions=True,)print(f"\n\n{result1=}")result1=01123456789101212345678910231234567891034123456789104512345678910561234567891067123456789107812345678910891234567891091012345678910dtype:objectresult2=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=056157258359460561662763864965dtype:int64# Updates the column after each iterationresult3=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=041183216733354671513436268175356810705921402Name: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)fromshapely.opsimportunary_unionimportshapelypolyshape=[]forkinrange(10):xmin=k*10+5ymin=k*10+5xmax=k*20+10ymax=k*20+10coordsalls=[[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=00POLYGON((55,105,1010,510,55))1POLYGON((1515,3015,3030,1530,1515))2POLYGON((2525,5025,5050,2550,2525))3POLYGON((3535,7035,7070,3570,3535))4POLYGON((4545,9045,9090,4590,4545))5POLYGON((5555,11055,110110,55110,5555))6POLYGON((6565,13065,130130,65130,6565))7POLYGON((7575,15075,150150,75150,7575))8POLYGON((8585,17085,170170,85170,8585))9POLYGON((9595,19095,190190,95190,9595))dfj=0POLYGON((55,510,1010,105,55))1POLYGON((5590,55110,65110,65130,7513...2POLYGON((4590,5590,55110,65110,65130...3POLYGON((5590,55110,65110,65130,7513...4POLYGON((3570,4570,4590,5590,55110,...5POLYGON((4570,4590,5590,55110,65110,...6POLYGON((4570,4590,5590,55110,65110,...7POLYGON((4590,5590,55110,65110,65130...8POLYGON((9055,9045,4545,4590,5590,5...9POLYGON((13065,11065,11055,5555,5511...dtype:objectdfj2=0POLYGON((55,105,1010,510,55))1POLYGON((4570,4590,5590,55110,65110,...2POLYGON((9045,7045,7035,5035,5025,3...3POLYGON((9045,7045,7035,5035,5025,3...4POLYGON((9045,7045,7035,5035,5025,3...5POLYGON((9045,7045,7035,5035,5025,3...6POLYGON((5025,3025,3015,1515,1530,2...7POLYGON((85150,85170,95170,95190,190...8POLYGON((85150,85170,95170,95190,190...9POLYGON((85150,85170,95170,95190,190...Name:0,dtype:object