There are at least 4 methods to subset row in Pandas dataframes.
Method 1: loc[[Comma]]
df.loc[[row_number1, row_number_2]]
Method 2: loc[Colon]
df.loc[row_number1: row_number_2]
Method 3: iloc[[Comma]]
df.iloc[[row_number1, row_number_2]]
Method 4: iloc[Colon]
df.iloc[[row_number1: row_number_2]]
Example 1 for Method 1
The following uses loc[[Comma]] (i.e., loc[[0,2]]
)to subset rows in a Pandas dataframe.
# importing Pandas module
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset two rows using loc[[Comma]]
selected_df=car_data.loc[[0,2]]
# print out the new dataframe with selected rows
print('Selected two rows: \n',selected_df)
The following shows the original dataframe and the selected rows using loc[[0,2]]
.
Original Dataframe: Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018 2 Tesla NY 2020 3 Ford MA 2019 4 Ford CA 2019 Selected two rows: Brand Location Year 0 Tesla CA 2019 2 Tesla NY 2020
Example 2 for Method 2
The following uses loc[colon] (i.e., loc[0:2]
)to subset rows in a Pandas dataframe.
# importing Pandas module
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset two rows using loc[colon]
selected_df=car_data.loc[0:2]
# print out the new dataframe with selected rows
print('Selected two rows: \n',selected_df)
The following shows the original dataframe and selected rows.
Note that, loc[] + colon (i.e., loc[0:2]
) has 3 rows, rather than 2 rows.
Original Dataframe: Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018 2 Tesla NY 2020 3 Ford MA 2019 4 Ford CA 2019 Selected two rows: Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018 2 Tesla NY 2020
Example 3 for Method 3
The following uses iloc[[comma]] (i.e., iloc[[0,2]]
)to subset rows in a Pandas dataframe.
# importing Pandas module
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset two rows using iloc[[comma]]
selected_df=car_data.iloc[[0,2]]
# print out the new dataframe with selected rows
print('Selected two rows: \n',selected_df)
The following shows the original dataframe and the selected rows using iloc[[0,2]]
.
Original Dataframe: Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018 2 Tesla NY 2020 3 Ford MA 2019 4 Ford CA 2019 Selected two rows: Brand Location Year 0 Tesla CA 2019 2 Tesla NY 2020
Example 4 for method 4
The following uses iloc[colon] (i.e., iloc[0:2]
) to subset rows from a Pandas dataframe.
# importing Pandas module
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset two rows using iloc[colon]
selected_df=car_data.iloc[0:2]
# print out the new dataframe with selected rows
print('Selected two rows: \n',selected_df)
The following shows the original dataframe and the selected rows using iloc[0:2]
.
Note that, iloc[0:2]
selects 2 rows (this example, namely Example 4), whereas loc[0:2]
selects 3 rows (Example 2).
Original Dataframe: Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018 2 Tesla NY 2020 3 Ford MA 2019 4 Ford CA 2019 Selected two rows: Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018