我是DataFrame的入门者。我有需要为DataFram编写查询的任务
这是我的数据框的样子:
我需要年龄最小且分数大于100的第一行。
我尝试使用min()函数,但我不知道如何使用其他查询?
如何建立DF
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
'Verstappen', 'Bottas', 'Ricciardo',
'Hulkenberg', 'Perez', 'Magnussen',
'Sainz', 'Alonso', 'Ocon', 'Leclerc',
'Grosjean', 'Gasly', 'Vandoorne',
'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
22, 21, 32, 22, 26, 28, 20, 29, 23]}
df = pd.DataFrame(dict1)
第3行的预期结果: Verstappen 249 21
因为他是个yun大的年龄,积分更大 100
谢谢你的帮助!
您可以为此使用df.loc
和df.idxmin()
:
查找具有的所有行,Points > 100
并从这些行中查找具有min
Age的行的索引:
In [3124]: ix = df[df.Points.gt(100)].Age.idxmin()
使用上面的索引从df中查找行:
In [3126]: df = df.loc[ix]
In [3127]: df
Out[3127]:
Driver Verstappen
Points 249
Age 21
Name: 3, dtype: object
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句