相当于熊猫中 SQL 的 LIMIT 和 OFFSET?

Pou*_*del 4 python pandas

我有一个这样的数据框:

   id type city
0   2    d    H
1   7    c    J
2   7    x    Y
3   2    o    G
4   6    i    F
5   5    b    E
6   6    v    G
7   8    u    L
8   1    g    L
9   8    k    U
Run Code Online (Sandbox Code Playgroud)

我想使用 Pandas 获得与 SQL 命令中类似的输出:

select id,type
from df
order by type desc
limit 4
offset 2
Run Code Online (Sandbox Code Playgroud)

需要的结果是:

   id type
0   8    u
1   2    o
2   8    k
3   6    i
Run Code Online (Sandbox Code Playgroud)

我试图按照官方教程https://pandas.pydata.org/pandas-docs/stable/comparison_with_sql.html#top-n-rows-with-offset

df.nlargest(4+2, columns='type').tail(4)
Run Code Online (Sandbox Code Playgroud)

但是,这失败了。

如何解决问题?

更新

import numpy as np
import pandas as pd
import pandasql as pdsql
from pandasql import sqldf
pysqldf = lambda q: sqldf(q, globals())
df = pd.read_csv('http://ourairports.com/data/airports.csv')


q = '''
select id,type
from df
order by type desc
limit 4
offset 2
'''

print(pysqldf(q))

```
       id           type
0    6525  small_airport
1  322127  small_airport
2    6527  small_airport
3    6528  small_airport
```
Run Code Online (Sandbox Code Playgroud)

使用熊猫:

print(df.sort_values('type', ascending=False).iloc[2:2+4][['id','type']])
           id           type
43740   37023  small_airport
43739   37022  small_airport
24046  308281  small_airport
24047  309587  small_airport
Run Code Online (Sandbox Code Playgroud)

Sco*_*ton 5

是的,整数位置,其中 iloc 起始索引是“偏移量”,结束索引以“限制”递增:

df.sort_values('type', ascending=False).iloc[2:6]
Run Code Online (Sandbox Code Playgroud)

输出:

   id type city
7   8    u    L
3   2    o    G
9   8    k    U
4   6    i    F
Run Code Online (Sandbox Code Playgroud)

您可以添加reset_index清理索引。

print(df.sort_values('type', ascending=False).iloc[2:6].reset_index(drop=True))
Run Code Online (Sandbox Code Playgroud)

输出:

   id type city
0   8    u    L
1   2    o    G
2   8    k    U
3   6    i    F
Run Code Online (Sandbox Code Playgroud)

更新让我们按类型和索引排序:

df.index.name = 'index'
df[['id','type']].sort_values(['type','index'], ascending=[False,True]).iloc[2:6]
Run Code Online (Sandbox Code Playgroud)

输出:

   index      id           type
0      3    6525  small_airport
1      5  322127  small_airport
2      6    6527  small_airport
3      7    6528  small_airport
Run Code Online (Sandbox Code Playgroud)