我想在熊猫数据框中每 5 行选择前两行。我怎么能这样做?
考虑以下数据框:
col1 | col2 | col3
1 | 1 | 1
2. | 2. | 2
3. | 3. | 3
4 | 4 | 4
5. | 5. | 5
6. | 6. | 6
7. | 7. | 7
Run Code Online (Sandbox Code Playgroud)
我想获得以下值
col1 | col2 | col3
1 | 1 | 1
2. | 2. | 2
Run Code Online (Sandbox Code Playgroud)
和
col1 | col2 | col3
6. | 6. | 6
7. | 7. | 7
Run Code Online (Sandbox Code Playgroud)
我将非常感谢您的帮助!!!
在索引groupby上使用楼层划分,并在其上使用head:
df.groupby(df.index//5).head(2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |