Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik Newsgroups: comp.lang.python Subject: Re: How to put back a number-based index Date: Fri, 13 May 2016 20:43:32 +0000 Lines: 172 Message-ID: References: <1878444350.3064027.1463156091513.JavaMail.yahoo.ref@mail.yahoo.com> <1878444350.3064027.1463156091513.JavaMail.yahoo@mail.yahoo.com> <1974768074.3081564.1463156374539.JavaMail.yahoo@mail.yahoo.com> <877815788.3201804.1463167634101.JavaMail.yahoo@mail.yahoo.com> <434110787.3205994.1463170295636.JavaMail.yahoo@mail.yahoo.com> <2098601859.3273341.1463170752937.JavaMail.yahoo@mail.yahoo.com> <1215747866.3212467.1463171634713.JavaMail.yahoo@mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de RvbaSiRg8DpWcAWomrNMvgtLWWk+SrVY8UhBIXM+AmHA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; '21,': 0.07; 'aggregate': 0.07; 'column': 0.07; 'finished.': 0.07; 'friday,': 0.07; 'subject:number': 0.07; 'subject:How': 0.09; '22,': 0.09; '34,': 0.09; '40,': 0.09; '[0,': 0.09; 'indexes': 0.09; 'rows': 0.09; 'typed': 0.09; 'email addr:python.org>': 0.10; 'index': 0.13; 'do,': 0.15; '>>>': 0.15; 'skip:f 30': 0.15; 'variables': 0.15; '(via': 0.16; '11.0,': 0.16; '2016': 0.16; '23,': 0.16; '24,': 0.16; '32,': 0.16; '33,': 0.16; '36,': 0.16; '37,': 0.16; '39,': 0.16; '42,': 0.16; '46,': 0.16; '6.0,': 0.16; 'aggregation,': 0.16; 'between.': 0.16; 'email addr:python.org>': 0.16; 'mean,': 0.16; 'michael,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:based': 0.16; 'wrote:': 0.16; 'example.': 0.18; 'email addr:gmail.com>': 0.18; '>>>': 0.20; 'to:name:python-list@python.org': 0.20; 'to:2**1': 0.21; '31,': 0.22; 'trying': 0.22; 'decide': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'command': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; '13,': 0.29; 'grouping': 0.29; 'preserve': 0.29; 'reset': 0.29; '15,': 0.30; 'operations': 0.31; 'help,': 0.32; 'operate': 0.32; 'changed': 0.33; 'michael': 0.33; 'values.': 0.33; 'handle': 0.34; 'skip:d 20': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'done': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'detail': 0.38; 'received:209': 0.38; 'skip:p 20': 0.38; 'mailing': 0.38; 'why': 0.39; 'data': 0.39; 'sure': 0.39; 'does': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'hello,': 0.40; 'your': 0.60; "you'll": 0.61; 'hope': 0.61; 'back': 0.62; 'providing': 0.62; '30,': 0.63; 'information': 0.63; '20,': 0.66; 'python-list': 0.66; 'email name:python-list': 0.67; 'regards.': 0.67; 'dear': 0.67; 'choose': 0.68; '26,': 0.72; 'interest.': 0.79; '10.0,': 0.84; '2\xc2\xa0': 0.84; '4.0,': 0.84; 'column.': 0.84; 'max,': 0.84; 'pasting': 0.84; 'to:addr:yahoo.co.uk': 0.84; 'different.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Ui0nbRwk/WrxaLVnaOvwaBjIXwJgaxduAiYPwN0SuvA=; b=MgFG8MqbolBMeC0t7QtJsZlexP5/CwBUcQJqz1p3HazTaRF/DsccvAIqVSOLRJX20w EwT3XnCZW+WSuHoEdWqXCv8Px+5/xs75Du5Sy0gJH/sX5I+WTODkmw2GiXGkSzfXggJ/ +jM6VejTGmQNGBP/duSKK1XI3CHp6K/zp3atlyOSg6VxaEsjFNQDcXWDGGhGpZrlzLla 0Hp2aFcuGAco74DOdThdCw9cZ209Twu465aJj4T9+V8m6TWLc44qF7p6muebeyHf4gN5 g77IfPVxug+yHXFwL9j+kdXkCrjLuX+a1HcrgAX+9uY6CZZJdzZiIrDVZSPhBThNbhcu oZTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Ui0nbRwk/WrxaLVnaOvwaBjIXwJgaxduAiYPwN0SuvA=; b=FfBXaxjlIN8qT1/Wy8gpoYVrW1eOlFYEk4NeOcDO8Kis+XeytGlV1pP5xIwaxikDJ8 bJRloaqDTxX3AoG3WWWr4bJXxKvX4tU9zc3cl0VEpQ0ED3LFKmdvhGuTVkiWaHuQeS2/ 6867iEJ0hpaO4k52fJGv3twldIirgomsIHzGgeSXtK4dF4UG54wxkoCkZCnWhDCCQY6m xTcOUGM+EnNtWH3db3dsCD5OekyP1eiUkT9OvjNJpM9+ZgcAYyAuiaHD6oLQ59PIziG8 /lj0wC8OKbu+gVUaOBaQOVqe4KVOrdz9sL6qyFD5+th8eOeCmVo7MZ0wew5ep07jZ+/F yhlw== X-Gm-Message-State: AOPr4FUnsSQAlKVYZ3ZTWiiWGrt46VM8AvwX2GqU29rL4igucxM8De5sRQPRriSg7LkVSL+//NArRlubZDBCQQ== X-Received: by 10.140.25.193 with SMTP id 59mr17690773qgt.77.1463172222188; Fri, 13 May 2016 13:43:42 -0700 (PDT) In-Reply-To: <1215747866.3212467.1463171634713.JavaMail.yahoo@mail.yahoo.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <1878444350.3064027.1463156091513.JavaMail.yahoo.ref@mail.yahoo.com> <1878444350.3064027.1463156091513.JavaMail.yahoo@mail.yahoo.com> <1974768074.3081564.1463156374539.JavaMail.yahoo@mail.yahoo.com> <877815788.3201804.1463167634101.JavaMail.yahoo@mail.yahoo.com> <434110787.3205994.1463170295636.JavaMail.yahoo@mail.yahoo.com> <2098601859.3273341.1463170752937.JavaMail.yahoo@mail.yahoo.com> <1215747866.3212467.1463171634713.JavaMail.yahoo@mail.yahoo.com> Xref: csiph.com comp.lang.python:108621 Here's an example. >>> import pandas as pd >>> df =3D pd.DataFrame({'group': list('AB') * 2, 'data': range(4)}, index=3Dlist('wxyz')) >>> df data group w 0 A x 1 B y 2 A z 3 B >>> df =3D df.reset_index() >>> df index data group 0 w 0 A 1 x 1 B 2 y 2 A 3 z 3 B >>> df.groupby('group').max() index data group A y 2 B z 3 If that doesn't help, you'll need to explain what you're trying to accomplish in detail -- what variables you started with, what transformations you want to do, and what variables you hope to have when finished. On Fri, May 13, 2016 at 4:36 PM David Shi wrote: > Hello, Michael, > > I changed groupby with one column. > > The index is different. > > Index([ u'AL', u'AR', u'AZ', u'CA', u'CO', u'CT', u'D= C', > u'DE', u'FL', u'GA', u'IA', u'ID', u'IL', u'I= N', > u'KS', u'KY', u'LA', u'MA', u'MD', u'ME', u'M= I', > u'MN', u'MO', u'MS', u'MT', u'NC', u'ND', u'N= E', > u'NH', u'NJ', u'NM', u'NV', u'NY', u'OH', u'O= K', > u'OR', u'PA', u'RI', u'SC', u'SD', u'State', u'T= N', > u'TX', u'UT', u'VA', u'VT', u'WA', u'WI', u'W= V', > u'WY'], > dtype=3D'object', name=3D0) > > > How to use this index? > > > Regards. > > > David > > > > On Friday, 13 May 2016, 21:19, David Shi wrote: > > > Hello, Michael, > > I typed in df.index > > I got the following > > MultiIndex(levels=3D[[1.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.= 0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, = 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.= 0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 53.0, 54.0, = 55.0, 56.0], [u'AL', u'AR', u'AZ', u'CA', u'CO', u'CT', u'DC', u'DE', u'FL'= , u'GA', u'IA', u'ID', u'IL', u'IN', u'KS', u'KY', u'LA', u'MA', u'MD', u'M= E', u'MI', u'MN', u'MO', u'MS', u'MT', u'NC', u'ND', u'NE', u'NH', u'NJ', u= 'NM', u'NV', u'NY', u'OH', u'OK', u'OR', u'PA', u'RI', u'SC', u'SD', u'Stat= e', u'TN', u'TX', u'UT', u'VA', u'VT', u'WA', u'WI', u'WV', u'WY']], > labels=3D[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1= 5, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, = 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48], [0, 2, 1, 3, 4= , 5, 7, 6, 8, 9, 11, 12, 13, 10, 14, 15, 16, 19, 18, 17, 20, 21, 23, 22, 24= , 27, 31, 28, 29, 30, 32, 25, 26, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 4= 5, 44, 46, 48, 47, 49]], > names=3D[u'StateFIPS', 0]) > > Regards. > > > David > > > > On Friday, 13 May 2016, 21:11, David Shi wrote: > > > Dear Michael, > > I have done a number of operation in between. > > Providing that information does not help you > > How to reset index after grouping and various operations is of interest. > > How to type in a command to find out its current dataframe? > > Regards. > > David > > > On Friday, 13 May 2016, 20:58, Michael Selik > wrote: > > > Just in case I misunderstood, why don't you make a little example of > before and after the grouping? This mailing list does not accept > attachments, so you'll have to make do with pasting a few rows of > comma-separated or tab-separated values. > > On Fri, May 13, 2016 at 3:56 PM Michael Selik > wrote: > > In order to preserve your index after the aggregation, you need to make > sure it is considered a data column (via reset_index) and then choose how > your aggregation will operate on that column. > > On Fri, May 13, 2016 at 3:29 PM David Shi wrote: > > Hello, Michael, > > Why reset_index before grouping? > > Regards. > > David > > > On Friday, 13 May 2016, 17:57, Michael Selik > wrote: > > > > > On Fri, May 13, 2016 at 12:27 PM David Shi via Python-list < > python-list@python.org> wrote: > > I lost my indexes after grouping in Pandas. > I managed to rest_index and got back the index column. > But How can I get back a index row? > > > Was the grouping an aggregation? If so, the original indexes are > meaningless. What you could do is reset_index before the grouping and whe= n > you aggregate decide how to handle the formerly-known-as-index column (mi= n, > max, mean, ?). > > > > > > > > >