我想在熊猫数据框中每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
我想获得以下值
col1 | col2 | col3
1 | 1 | 1
2. | 2. | 2
和
col1 | col2 | col3
6. | 6. | 6
7. | 7. | 7
我将非常感谢您的帮助!
答案 0 :(得分:4)
对索引使用下限划分,并在groupby
上对其进行head
:
df.groupby(df.index//5).head(2)