在 Python Pandas 中選擇兩個索引值之間的資料框行
我們可以切片一個 Pandas 資料框來選擇兩個索引值之間的行。讓我們舉個例子,看看它是如何完成的。
步驟
- 建立一個二維、大小可變、潛在異構的表格資料,df。
- 列印輸入資料框,df。
- 初始化一個變數,表示索引的下限。
- 初始化另一個變數,表示索引的上限。
- 使用 df[index_lower_limit: index_upper_limit] 來列印範圍索引中的資料框。
示例
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) print "Input DataFrame is:\n", df index_lower_limit = 1 index_upper_limit = 3 print("DataFrame between two index values:\n", df[index_lower_limit: index_upper_limit])
輸出
Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 DataFrame between two index values: x y z 1 2 7 3 2 7 5 5
廣告