Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nth() mixes column order #20760

Closed
sursu opened this issue Apr 20, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@sursu
Copy link

commented Apr 20, 2018

Consider the following dataframe:

df = pd.DataFrame([[179293473,'2016-06-01 00:00:03.549745','http://www.dr.dk/nyheder/',39169523],[179293473,'2016-06-01 00:04:22.346018','https://www.information.dk/indland/2016/05/hvert-tredje-offer-naar-anmelde-voldtaegt-tide', 39125224],
 [179773461, '2016-06-01 22:13:16.588146', 'https://www.google.dk', 31658124],
 [179773461, '2016-06-01 22:14:04.059781', 'https://www.google.dk', 31658124],
 [179773461, '2016-06-01 22:16:37.230587', np.nan, 31658124],
 [179773461, '2016-06-01 22:23:09.847149', 'https://www.google.dk', 32718401],
 [179773461, '2016-06-01 22:23:55.158929', np.nan, 32718401],
 [179773461, '2016-06-01 22:27:00.857224', np.nan, 32718401]],
columns=['SessionID', 'PageTime', 'ReferrerURL', 'PageID'])

which looks like this:

 SessionID PageTime ReferrerURL PageID
179293473 2016-06-01 00:00:03.549745 http://www.dr.dk/nyheder/ 39169523
179293473 2016-06-01 00:04:22.346018 https://www.information.dk/ 39125224
179773461 2016-06-01 22:13:16.588146 https://www.google.dk 31658124
179773461 2016-06-01 22:14:04.059781 https://www.google.dk 31658124
179773461 2016-06-01 22:16:37.230587 NaN 31658124
179773461 2016-06-01 22:23:09.847149 https://www.google.dk 32718401
179773461 2016-06-01 22:23:55.158929 NaN 32718401
179773461 2016-06-01 22:27:00.857224 NaN 32718401

Run:
df.groupby('SessionID').nth(-1)

Out:

 SessionID PageID PageTime ReferrerURL
179293473 39125224 2016-06-01 00:04:22.346018 https://www.information.dk/
179773461 32718401 2016-06-01 22:27:00.857224 NaN

Question: Why has nth() mixed the order of my columns?

@ron819

This comment has been minimized.

Copy link

commented Aug 9, 2018

@mroeschke @jreback
Not sure if this is a bug but it would be nice if the nth() won't change the orders of the columns.

@reidy-p reidy-p referenced this issue Sep 23, 2018

Merged

BUG: Maintain column order with groupby.nth #22811

3 of 4 tasks complete

@jreback jreback added this to the 0.24.0 milestone Sep 23, 2018

@jreback jreback added the Reshaping label Sep 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.