DataFrame slicing using iloc in Pandas
.iloc selects data only by integer location:
import pandas as pd df = pd.DataFrame({'Age': [30, 20, 22, 40, 32, 28, 39], 'Color': ['Blue', 'Green', 'Red', 'White', 'Gray', 'Black', 'Red'], 'Food': ['Steak', 'Lamb', 'Mango', 'Apple', 'Cheese', 'Melon', 'Beans'], 'Height': [165, 70, 120, 80, 180, 172, 150], 'Score': [4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2], 'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'] }, index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean', 'Christina', 'Cornelia']) print("\n -- Selecting a single row with .iloc with an integer -- \n") print(df.iloc[4]) print("\n -- Selecting multiple rows with .iloc with a list of integers -- \n") print(df.iloc[[2, -2]]) print("\n -- Selecting multiple rows with .iloc with slice notation -- \n") print(df.iloc[:5:3])
C:\python\pandas examples>python example13.py -- Selecting a single row with .iloc with an integer -- Age 32 Color Gray Food Cheese Height 180 Score 1.8 State AK Name: Dean, dtype: object -- Selecting multiple rows with .iloc with a list of integers -- Age Color Food Height Score State Aaron 22 Red Mango 120 9.0 FL Christina 28 Black Melon 172 9.5 TX -- Selecting multiple rows with .iloc with slice notation -- Age Color Food Height Score State Jane 30 Blue Steak 165 4.6 NY Penelope 40 White Apple 80 3.3 AL C:\python\pandas examples>
2018-10-11T06:34:07+05:30
2018-10-11T06:34:07+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution