🏠 Data Science Guides DataFrame Indexing: .loc[] vs .iloc[] The loc
and iloc
functions are commonly used to select certain groups of rows (and columns) of a pandas DataFrame.
To explore these two functions and their differences, we'll use a DataFrame of 7 drinks with different features and nutrition facts:
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n \n# Set the row label indexes to the "drink" column:\ndf.set_index('drink', inplace = True)\ndf Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
soda
True
cold
10.5
150
coffee
False
hot
3.0
31
smoothie
False
cold
6.0
85
water
False
cold
0.0
0
tea
False
hot
2.0
43
lemonade
False
cold
9.5
125
slushy
False
cold
8.0
99
Difference Between loc
and iloc
The difference between the loc
and iloc
functions is that the loc
function selects rows using row labels (e.g. tea
) whereas the iloc
function selects rows using their integer positions (staring from 0
and going up by one for each row).
Selecting a Single Row of Data Selecting a Single Row by the Row Label (.loc
) For the loc
function, specifying the row label as the input will index only the row with that label.
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\ndf.loc['tea'] Run Code
Reset Code Python Output:
```
carbonated? False
temperature hot
sugar(tsp) 2.0
calories 43
Name: tea, dtype: object
```
Selecting a Single Row by the Integer Index (.iloc
) On the other hand, for the iloc
function, specifying a single integer index as the input will index the row at that position. For example, starting with zero, index 2
refers to the 3rd element in the list (the smoothie
row):
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# Retrieving the row at index 2\ndf.iloc[2] Run Code
Reset Code Python Output:
```
carbonated? False
temperature cold
sugar(tsp) 6.0
calories 85
Name: smoothie, dtype: object
```
Selecting a Single Row as a DataFrame Note that the python code above returns the row as a Series. If we want the row returned as a DataFrame, we use a list with only one element:
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\ndf.loc[ ['tea'] ] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
tea
False
hot
2.0
43
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\ndf.iloc[ [1] ] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
coffee
False
hot
3.0
31
Selecting Multiple Rows of Data Selecting Multiple Rows using a List For both the loc
and iloc
functions, we can use a list as our input to retrieve multiple rows. The labels of the output are ordered by when the label appears in the input list.
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\n# note that multiple labels along the vertical axis is specified\ndf.loc[['smoothie', 'tea', 'soda']] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
smoothie
False
cold
6.0
85
tea
False
hot
2.0
43
soda
True
cold
10.5
150
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\n# note that the range is between 0 and (len(df) - 1) inclusive\ndf.iloc[[2, 4, 5]] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
smoothie
False
cold
6.0
85
tea
False
hot
2.0
43
lemonade
False
cold
9.5
125
Selecting Multiple Rows using a Slice Alternatively, we can use a slice object as our input to retrieve multiple rows. However, it is important to note that for the loc
function, the start and stop are included in the output whereas the stop is not included for the iloc
function.
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\n# note that 'coffee' and 'tea' are included in the output\ndf.loc['coffee':'tea'] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
coffee
False
hot
3.0
31
smoothie
False
cold
6.0
85
water
False
cold
0.0
0
tea
False
hot
2.0
43
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\n# note that the output includes the row at index 3 but not the row at index 4\ndf.iloc[0:4] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
calories
drink
soda
True
cold
10.5
150
coffee
False
hot
3.0
31
smoothie
False
cold
6.0
85
water
False
cold
0.0
0
Selecting for BOTH Rows and Columns Selecting a Single Cell Value For the
loc
function, to find a specific cell, we can use a list as the input value, specifying the vertical axis label (left) and the horizontal axis label (right).
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\n# 'coffee' is the vertical axis labe and 'temperature' is the horizontal axis label\ndf.loc['coffee', 'temperature'] Run Code
Reset Code Python Output:
For the
iloc
function, to find a specific cell, we can specify the row index (left) and column index (right) separated by a comma.
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\n# note that the following code uses single brackets ([]). Double brackets ([[]]) will retrieve rows between the specified integers.\ndf.iloc[0,2] Run Code
Reset Code Python Output:
Selecting a Multiple Cell Values using a List Similar to indexing only rows, we can retrieve multiple rows and columns. One way to do this is using two lists (one for the vertical axis labels and one for the horizontal axis labels) separated by a comma as the input:
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\ndf.loc[['water', 'lemonade'], ['sugar(tsp)', 'calories']] Run Code
Reset Code Python Output:
sugar(tsp)
calories
drink
water
0.0
0
lemonade
9.5
125
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\ndf.iloc[[1, 0], [2, 3]] Run Code
Reset Code Python Output:
sugar(tsp)
calories
drink
coffee
3.0
31
soda
10.5
150
Selecting a Muliple Cell Values using a Slice Additionally, we can use slice objects:
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\ndf.loc[:'water', 'carbonated?':'sugar(tsp)'] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
drink
soda
True
cold
10.5
coffee
False
hot
3.0
smoothie
False
cold
6.0
water
False
cold
0.0
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\ndf.iloc[3:7, 2:] Run Code
Reset Code Python Output:
sugar(tsp)
calories
drink
water
0.0
0
tea
2.0
43
lemonade
9.5
125
slushy
8.0
99
Selecting a Muliple Cell Values using a Combination of Everything We can use a combination of the two methods together:
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\ndf.set_index('drink', inplace=True)\n# loc function\ndf.loc[['coffee', 'smoothie', 'soda'], :'sugar(tsp)'] Run Code
Reset Code Python Output:
carbonated?
temperature
sugar(tsp)
drink
coffee
False
hot
3.0
smoothie
False
cold
6.0
soda
True
cold
10.5
import pandas as pd\n \n# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:\ndf = pd.DataFrame([\n {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},\n {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31},\n {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85},\n {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},\n {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43},\n {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},\n {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},\n])\n# iloc function\ndf.iloc[2:5, [0, 1, 3]] Run Code
Reset Code Python Output:
carbonated?
temperature
calories
drink
smoothie
False
cold
85
water
False
cold
0
tea
False
hot
43
Pandas Documentation Click Here for the full pandas documentation for the loc
functionClick Here for the full pandas documentation for the iloc
function