Python 访问排序列表的第一个元素

Python 访问排序列表的第一个元素,python,python-3.x,pandas,list,dataframe,Python,Python 3.x,Pandas,List,Dataframe,下表为原始表格: identifier comments_count 2353962646372849000 4153 2353962646372849028 6102 2342365172189273063 3936 2353962646372849567 5202 2342365172189273168 3076 我想访问dfsorted.identifier的第一个元素,它是一个排序列表 查询的输

下表为原始表格:

identifier            comments_count
2353962646372849000        4153
2353962646372849028        6102
2342365172189273063        3936
2353962646372849567        5202
2342365172189273168        3076
我想访问dfsorted.identifier的第一个元素,它是一个排序列表

查询的输出应为
'2353962646372849028'

dfsorted=df.sort\u值('comments\u count',升序=False)

询问

dfsorted['identifier'][0] 
但是,会不断返回
'2353962646372849000'
(排序前标识符的第一个元素)。 如何解决这个问题

dfsorted = df.sort_values('comments_count', ascending=False).reset_index(drop=True)
使用此选项重置索引:D

对列表进行排序不会更新索引,如果打印该表,您将看到索引没有更新。通过使用
reset\u index(drop=True)
可以创建新索引并删除旧索引。如果省略
drop=True
,则会将旧索引保存在一个额外的列中。

您可以使用:

dfsorted.iloc[0,:]['identifier']
或更简单:

dfsorted.iloc[0,0]
结果:

2353962646372849028
重置索引将正常工作,结果如您所愿:

2342365172189273168

使用
dfsorted=df.sort\u值('comments\u count',ascending=False,ignore\u index=True)
2353962646372849028
dfs=df.groupby('comments_count', as_index=False).apply(lambda x: x.nlargest(1, columns=['identifier'])).reset_index(level=1, drop=1)
2342365172189273168