Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: David Shi Newsgroups: comp.lang.python Subject: Re: How to put back a number-based index Date: Fri, 13 May 2016 21:51:58 +0000 (UTC) Lines: 171 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> <2075459214.3318248.1463172701996.JavaMail.yahoo@mail.yahoo.com> <1584945027.3340053.1463176318146.JavaMail.yahoo@mail.yahoo.com> Reply-To: David Shi Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de ybmIM3/LJsZT8pD65qqnhQ+tOf6Sdnk3hLwJfaszteBg== 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; 'float': 0.05; 'from:addr:yahoo.co.uk': 0.05; '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; 'label,': 0.09; 'rows': 0.09; 'specifying': 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; 'from:addr:davidgshi': 0.16; 'from:name:david shi': 0.16; 'int64': 0.16; 'mean,': 0.16; 'michael,': 0.16; 'now.\xc2\xa0': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reply-to:addr:davidgshi': 0.16; 'reply- to:name:david shi': 0.16; 'row': 0.16; 'subject:based': 0.16; 'wrote:': 0.16; 'string': 0.17; 'example.': 0.18; 'integer': 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; '14,': 0.27; '13,': 0.29; 'grouping': 0.29; 'preserve': 0.29; 'convert': 0.29; 'reset': 0.29; '15,': 0.30; 'operations': 0.31; 'help,': 0.32; 'operate': 0.32; 'changed': 0.33; 'michael': 0.33; 'clarify': 0.33; 'values.': 0.33; 'handle': 0.34; 'skip:d 20': 0.34; 'list': 0.34; 'so,': 0.35; 'header:Received:8': 0.35; 'could': 0.35; 'done': 0.35; 'label': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'detail': 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; 'avoid': 0.61; 'back': 0.62; 'providing': 0.62; 'skip:n 10': 0.62; '30,': 0.63; 'information': 0.63; '20,': 0.66; 'python-list': 0.66; 'regards.': 0.67; 'header :Reply-To:1': 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; 'message-id:@mail.yahoo.com': 0.84; 'pasting': 0.84; 'received:98.138.100': 0.84; 'received:98.138.229': 0.84; 'different.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.co.uk; s=s2048; t=1463176497; bh=ngmpyyRPL8mL44CDIToKWhOZMluO+36wLLtcsB17e7k=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=QswJJ+KuDyn0eYVtu0nfNRL9/ZIo9LoI5RcdJCoWzIOr1HDzKtR8V3BRLreoyyjmJ5Hfeyz6GmWPuI1QDYxWkTDDrDNntewg3HsEmuZrIaACVnq+8IZS4oHPHPEANIVsCDMEgoFK9qxcbz3UQhEt6k8u4lqBtQItz6R4x24k8K3WucRxTcBpvQjzf+ShHJr46MTSl7JHB5YDPDSJkmbkjtzkwHMAm5Twkoxrqd/vLhvW5kll1XwLD46nHfV1b2mG0kcLT+gnhJTfXR8bxjEk6/Oh56D6/eC1PknoVjl+WIt2Ymt6kuemhUP68Qu0kuZXPJljVD3atrnmewIvyf9jVw== X-Yahoo-Newman-Property: ymail-4 X-Yahoo-Newman-Id: 922855.95529.bm@omp1038.mail.ir2.yahoo.com X-YMail-OSG: .CCm8jQVM1mCFUYqShBQJGmsedOpmiXDoi3t3yS5SKiscssnhHZZC9Zrkq2wsSF fXr4ko6cHoRW4DQpWMz6i3Pmq_ocjKrpXqShhENJk3NzqpRsEw47lYBTNdeV8ajE62G31szvfUWr eMjbgXwT9fZ.7LBBSr.GY3uzgU4NGsHYu511as7Q9eivCG4wbCSczEbNYplpfHl6IsTuYkLEneul OvxSQ0H6UiebLDVHXvOejxwu5hPlchkmtVJrA6SENN0lG83R2GZ1npu0DZOINxOnom_EFuOZE9Lh JaL2oVhtEef3RIQc40ARxc0FFMlZV0eYyYBMDJYr_7fX2ACY.bwz60KU7zLuBDK1NemJ7EIeVW25 NCmPSxOf7.mBFC5J9Dz32VPrIMnHtOwyGAH94txLtJwWsdsgQmzUL_7MRmmMneoH45hsXGJTtwEf Bp4GoETo.44yE.gEnhJnu11_KL1N1iMeQAqt7Ykfnk2TXyq7I9q5Aeeaj0h2e.hFjzjOuNXJF36n 7oy_e_BKSt9f7U1eadfBnqRok.OewE58HUDfZKKVIORA2zA-- In-Reply-To: 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: <1584945027.3340053.1463176318146.JavaMail.yahoo@mail.yahoo.com> 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> <2075459214.3318248.1463172701996.JavaMail.yahoo@mail.yahoo.com> Xref: csiph.com comp.lang.python:108625 Hello, Michael, How to convert a float type column into an integer or label or string type?= =20 On Friday, 13 May 2016, 22:02, Michael Selik = wrote: =20 To clarify that you're specifying the index as a label, use df.iloc =C2=A0 =C2=A0 >>> df =3D pd.DataFrame({'X': range(4)}, index=3Dlist('abcd')= )=C2=A0 =C2=A0 >>> df=C2=A0 =C2=A0 =C2=A0 =C2=A0X=C2=A0 =C2=A0 a =C2=A00=C2= =A0 =C2=A0 b =C2=A01=C2=A0 =C2=A0 c =C2=A02=C2=A0 =C2=A0 d =C2=A03=C2=A0 = =C2=A0 >>> df.loc['a']=C2=A0 =C2=A0 X =C2=A0 =C2=A00=C2=A0 =C2=A0 Name: a, = dtype: int64=C2=A0 =C2=A0 >>> df.iloc[0]=C2=A0 =C2=A0 X =C2=A0 =C2=A00=C2= =A0 =C2=A0 Name: a, dtype: int64 On Fri, May 13, 2016 at 4:54 PM David Shi wrote: Dear Michael, To avoid complication, I only groupby using one column. It is OK now.=C2=A0 But, how to refer to new row index?=C2=A0 How do I use = floating index? Float64Index([ 1.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 1= 6.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 2= 7.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 3= 8.0, 39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 5= 0.0, 51.0, 53.0, 54.0, 55.0, 56.0], dtype=3D'float64', name=3Du'StateFIPS') Regards. David=20 On Friday, 13 May 2016, 21:43, Michael Selik = wrote: =20 Here's an example. =C2=A0 =C2=A0 >>> import pandas as pd=C2=A0 =C2=A0 >>> df =3D pd.DataFrame(= {'group': list('AB') * 2, 'data': range(4)}, index=3Dlist('wxyz'))=C2=A0 = =C2=A0 >>> df=C2=A0 =C2=A0 =C2=A0 =C2=A0data group=C2=A0 =C2=A0 w =C2=A0 = =C2=A0 0 =C2=A0 =C2=A0 A=C2=A0 =C2=A0 x =C2=A0 =C2=A0 1 =C2=A0 =C2=A0 B=C2= =A0 =C2=A0 y =C2=A0 =C2=A0 2 =C2=A0 =C2=A0 A=C2=A0 =C2=A0 z =C2=A0 =C2=A0 3= =C2=A0 =C2=A0 B=C2=A0 =C2=A0 >>> df =3D df.reset_index()=C2=A0 =C2=A0 >>> = df=C2=A0 =C2=A0 =C2=A0 index =C2=A0data group=C2=A0 =C2=A0 0 =C2=A0 =C2=A0 = w =C2=A0 =C2=A0 0 =C2=A0 =C2=A0 A=C2=A0 =C2=A0 1 =C2=A0 =C2=A0 x =C2=A0 =C2= =A0 1 =C2=A0 =C2=A0 B=C2=A0 =C2=A0 2 =C2=A0 =C2=A0 y =C2=A0 =C2=A0 2 =C2=A0= =C2=A0 A=C2=A0 =C2=A0 3 =C2=A0 =C2=A0 z =C2=A0 =C2=A0 3 =C2=A0 =C2=A0 B=C2= =A0 =C2=A0 >>> df.groupby('group').max()=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = index =C2=A0data=C2=A0 =C2=A0 group=C2=A0 =C2=A0 A =C2=A0 =C2=A0 =C2=A0 =C2= =A0 y =C2=A0 =C2=A0 2=C2=A0 =C2=A0 B =C2=A0 =C2=A0 =C2=A0 =C2=A0 z =C2=A0 = =C2=A0 3 If that doesn't help, you'll need to explain what you're trying to accompli= sh in detail -- what variables you started with, what transformations you w= ant 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'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'ME', 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'State', u'TN'= , u'TX', u'UT', u'VA', u'VT', u'WA', u'WI', u'WV'= , u'WY'], dtype=3D'object', name=3D0) How to use this index? Regards. David=20 On Friday, 13 May 2016, 21:19, David Shi wrote: =20 Hello, Michael, I typed in df.index I got the followingMultiIndex(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, 2= 5.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, 5= 1.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'ME', 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'S= C', u'SD', u'State', 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, 15,= 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, 45,= 44, 46, 48, 47, 49]], names=3D[u'StateFIPS', 0])Regards. David=20 On Friday, 13 May 2016, 21:11, David Shi wrote: =20 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=20 On Friday, 13 May 2016, 20:58, Michael Selik = wrote: =20 Just in case I misunderstood, why don't you make a little example of befor= e 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-s= eparated values. On Fri, May 13, 2016 at 3:56 PM Michael Selik wro= te: In order to preserve your index after the aggregation, you need to make sur= e 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=20 On Friday, 13 May 2016, 17:57, Michael Selik wr= ote: =20 =20 On Fri, May 13, 2016 at 12:27 PM David Shi via Python-list 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 meaningles= s. What you could do is reset_index before the grouping and when you aggreg= ate decide how to handle the formerly-known-as-index column (min, max, mean= , ?). =20 =20 =20 =20 =20