Package: a-pandas-ex-enumerate-groups
Groups values in a column of a DataFrame and enumerates thempipinstalla-pandas-ex-enumerate_groupsfroma_pandas_ex_enumerate_groupsimportpd_add_enumerate_grouppd_add_enumerate_group()importpandasaspddf=pd.read_csv("https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv")dfi=df.ds_enumerate_groups(enumerated_column="Age_enu",column_to_enumerate="Age")print(dfi[:20].to_string())dfi=df.ds_enumerate_groups(enumerated_column="Cabin_en",column_to_enumerate="Cabin")print(dfi[:20].to_string())dfi=df.ds_enumerate_groups(enumerated_column="Fare_en",column_to_enumerate="Fare")print(dfi[:20].to_string())PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarkedAge_enu80380413Thomas,Master.AssadAlexandermale0.420126258.5167NaNC075575612Hamalainen,Master.Viljomale0.671125064914.5000NaNS164464513Baclini,Miss.Eugeniefemale0.7521266619.2583NaNC246947013Baclini,Miss.HeleneBarbarafemale0.7521266619.2583NaNC2787912Caldwell,Master.AldenGatesmale0.830224873829.0000NaNS383183212Richards,Master.GeorgeSibleymale0.83112910618.7500NaNS330530611Allison,Master.HudsonTrevormale0.9212113781151.5500C22C26S482782812Mallet,Master.Andremale1.0002S.C./PARIS207937.0042NaNC538138213Nakid,Miss.Maria("Mary")female1.0002265315.7417NaNC516416503Panula,Master.EinoViljamimale1.0041310129539.6875NaNS518318412Becker,Master.RichardFmale1.002123013639.0000F4S538638703Goodwin,Master.SidneyLeonardmale1.0052CA214446.9000NaNS517217313Johnson,Miss.EleanorIleenfemale1.001134774211.1333NaNS578878913Dean,Master.BertramVeremale1.0012C.A.231520.5750NaNS564264303Skoog,Miss.MargitElizabethfemale2.003234708827.9000NaNS67803Palsson,Master.GostaLeonardmale2.003134990921.0750NaNS653053112Quick,Miss.PhyllisMayfemale2.00112636026.0000NaNS629729801Allison,Miss.HelenLorainefemale2.0012113781151.5500C22C26S682482503Panula,Master.UrhoAbrahammale2.0041310129539.6875NaNS620520603Strom,Miss.TelmaMatildafemale2.000134705410.4625G6S6PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarkedCabin_en58358401Ross,Mr.JohnHugomale36.0001304940.1250A10C047547601Clifford,Mr.GeorgeQuincymaleNaN0011046552.0000A14S155655711DuffGordon,Lady.(LucilleChristianaSutherland)("Mrs Morgan")female48.0101175539.6000A16C228428501Smith,Mr.RichardWilliammaleNaN0011305626.0000A19S359960011DuffGordon,Sir.CosmoEdmund("Mr Morgan")male49.010PC1748556.9292A20C463063111Barkworth,Mr.AlgernonHenryWilsonmale80.0002704230.0000A23S586786801Roebling,Mr.WashingtonAugustusIImale31.000PC1759050.4958A24S664764811Simonius-Blumer,Col.OberstAlfonsmale56.0001321335.5000A26C720921011Blank,Mr.Henrymale40.00011227731.0000A31C818518601Rood,Mr.HughRoscoemaleNaN0011376750.0000A32S944544611Dodge,Master.Washingtonmale4.0023363881.8583A34S1080680701Andrews,Mr.ThomasJrmale39.0001120500.0000A36S11969701Goldschmidt,Mr.GeorgeBmale71.000PC1775434.6542A5C12232411Sloper,Mr.WilliamThompsonmale28.00011378835.5000A6S1317417501Smith,Mr.JamesClinchmale56.0001776430.6958A7C1473773811Lesurer,Mr.GustaveJmale35.000PC17755512.3292B101C1581581601Fry,Mr.RichardmaleNaN001120580.0000B102S1652352411Hippach,Mrs.LouisAlbert(IdaSophiaFischer)female44.00111136157.9792B18C1732933011Hippach,Miss.JeanGertrudefemale16.00111136157.9792B18C1717017101Vanderhoef,Mr.Wyckoffmale61.00011124033.5000B19S18PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarkedFare_en27127213Tornquist,Mr.WilliamHenrymale25.000LINE0.0000NaNS059759803Johnson,Mr.Alfredmale49.000LINE0.0000NaNS030230303Johnson,Mr.WilliamCahooneJrmale19.000LINE0.0000NaNS063363401Parr,Mr.WilliamHenryMarshmaleNaN001120520.0000NaNS027727802Parkes,Mr.Francis"Frank"maleNaN002398530.0000NaNS041341402Cunningham,Mr.AlfredFlemingmaleNaN002398530.0000NaNS067467502Watson,Mr.EnnisHastingsmaleNaN002398560.0000NaNS026326401Harrison,Mr.Williammale40.0001120590.0000B94S046646702Campbell,Mr.WilliammaleNaN002398530.0000NaNS073273302Knight,Mr.RobertJmaleNaN002398550.0000NaNS017918003Leonard,Mr.Lionelmale36.000LINE0.0000NaNS080680701Andrews,Mr.ThomasJrmale39.0001120500.0000A36S048148202Frost,Mr.AnthonyWood"Archie"maleNaN002398540.0000NaNS082282301Reuchlin,Jonkheer.JohnGeorgemale38.000199720.0000NaNS081581601Fry,Mr.RichardmaleNaN001120580.0000B102S037837903Betros,Mr.Tannousmale20.00026484.0125NaNC187287301Carlsson,Mr.FransOlofmale33.0006955.0000B51B53B55S232632703Nysveen,Mr.JohanHansenmale61.0003453646.2375NaNS384384403Lemberopolous,Mr.PeterLmale34.50026836.4375NaNC481881903Holm,Mr.JohnFredrikAlexandermale43.000C70756.4500NaNS5df.d_enumerate_all_groups_in_all_columns(prefix="group_")Out[4]:PassengerIdSurvivedPclass...group_Faregroup_Cabingroup_Embarked0103...18NaN2.01211...20781.00.02313...41NaN2.03411...18955.02.04503...43NaN2.0.......................88688702...85NaN2.088788811...15330.02.088888903...131NaN2.088989011...15360.00.089089103...30NaN1.0[891rowsx24columns]df.d_enumerate_groups_in_multiple_columns(columns=[['Pclass','Survived']],prefix="group_",name_sep="_")Out[7]:PassengerIdSurvivedPclass...CabinEmbarkedgroup_Pclass_Survived0103...<NA>S41211...C85C12313...<NA>S53411...C123S14503...<NA>S4.......................88688702...<NA>S288788811...B42S188888903...<NA>S488989011...C148C189089103...<NA>Q4[891rowsx13columns]