The .loc[] function in Pandas is a powerful tool for data indexing and selection. With the Boston Housing Prices dataset, you can use .loc[] in various ways to perform data manipulations. Here are all possible usages of .loc[] with the dataset:
# Usage of loc
# 1. Selecting specific rows and columns by labels
# 16. Selecting rows and specific columns based on complex conditions
subset_16=boston_df.loc[
(boston_df['CRIM'] <1) & (boston_df['RM'] >6),
['CRIM', 'RM', 'PRICE']
]
A complete usage of iloc
The .iloc[] function in Pandas is used for integer-location based indexing, which means you can use integer positions to select rows and columns from a DataFrame. Here are all possible usages of .iloc[] with the Boston Housing Prices dataset:
# Usage of iloc
# 1. Selecting a single row by integer index
subset_1=boston_df.iloc[5]
# 2. Selecting multiple rows by integer index
subset_2=boston_df.iloc[[0, 5, 10]]
# 3. Selecting specific rows and columns by integer index
subset_3=boston_df.iloc[[0, 5, 10], [0, 5, -1]]
# 4. Selecting all rows for specific columns by integer index
subset_4=boston_df.iloc[:, [0, 5, -1]]
# 5. Selecting a single cell value by integer index
cell_value=boston_df.iloc[0, 3]
# 6. Modifying a single cell value using iloc[]
boston_df.iloc[0, 3] =0.5
# 7. Selecting rows and specific columns using integer slices
subset_7=boston_df.iloc[10:20, 2:6]
# 8. Selecting rows and all columns using integer slices
subset_8=boston_df.iloc[10:20, :]
# 9. Selecting all rows and specific columns using integer slices
subset_9=boston_df.iloc[:, 2:6]
# 10. Selecting rows using a boolean mask with iloc[]
댓글