To select multiple rows from a DataFrame, set the range using the : operator. At first, import the require pandas library with alias −
import pandas as pd
Now, create a new Pandas DataFrame −
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]],index=['w', 'x', 'y', 'z'],columns=['a', 'b'])
Select multiple rows using the : operator −
dataFrame[0:2]
Example
Following is the code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]],index=['w', 'x', 'y', 'z'],columns=['a', 'b']) # DataFrame print"DataFrame...\n",dataFrame # select rows with loc print"\nSelect rows by passing label..." print(dataFrame.loc['z']) # select rows with integer location using iloc print"\nSelect rows by passing integer location..." print(dataFrame.iloc[1]) # selecting multiple rows print"\nSelect multiple rows..." print(dataFrame[0:2])
Output
This will produce the following output −
DataFrame... a b w 10 15 x 20 25 y 30 35 z 40 45 Select rows by passing label... a 40 b 45 Name: z, dtype: int64 Select rows by passing integer location... a 20 b 25 Name: x, dtype: int64 Select multiple rows... a b w 10 15 x 20 25