Luc
Luc

Reputation: 747

Access the first element of a sorted list

Below is the original table:

identifier            comments_count
2353962646372849000        4153
2353962646372849028        6102
2342365172189273063        3936
2353962646372849567        5202
2342365172189273168        3076
df = pd.DataFrame({

    'identifier': [2353962646372849000, 2353962646372849028, 2342365172189273063, 2353962646372849567 , 2342365172189273168],

    'comments_count': [4153, 6102, 3936, 5202, 3076],
})

I would like to access the first element of the dfsorted.identifier, which is a sorted list.

The query should have an output of '2353962646372849028'.

dfsorted = df.sort_values('comments_count',ascending=False)

identifier            comments_count
2353962646372849028        6102
2353962646372849567        5202
2353962646372849000        4153
2342365172189273063        3936
2342365172189273168        3076

The query

dfsorted['identifier'][0] 

however keeps returning '2353962646372849000' (the first element of the identifier before the sorting). How to fix the issue?

Upvotes: 1

Views: 922

Answers (3)

Noah
Noah

Reputation: 632

dfsorted = df.sort_values('comments_count', ascending=False).reset_index(drop=True)

Use this to reset the indexes :D

Sorting a List doesn't update the indexes, if you print the Table you will see the indexes weren't renewed. By using reset_index(drop=True) you create new indexes and drop the old ones. If you leave out drop=True it saves the old indexes in an extra column.

Upvotes: 0

Evair Silvester
Evair Silvester

Reputation: 23

dfs=df.groupby('comments_count', as_index=False).apply(lambda x: x.nlargest(1, columns=['identifier'])).reset_index(level=1, drop=1)

The reset index will work fine, the result come as you wish:

2342365172189273168

Upvotes: 0

ipj
ipj

Reputation: 3598

You may use:

dfsorted.iloc[0,:]['identifier']

or simpler:

dfsorted.iloc[0,0]

result:

2353962646372849028

Upvotes: 1

Related Questions