are labeled as shown.
Now, we are using loc[] and iloc[] to select a specific row from the DataFrame. Please note that when we select a row using loc[] or iloc[], the returned value is a pandas Series. And as we discussed, loc[] selects a row using its label, where iloc[] selects a row using its index.
series1 = df1.loc["Row 1"] series2 = df1.iloc[0] print("series1: \n", series1) print("series2: \n", series2)
So, the output of the above part of the program will be:
series1: Column 1 1 Column 2 2 Column 3 3 Name: Row 1, dtype: int64 series2: Column 1 1 Column 2 2 Column 3 3 Name: Row 1, dtype: int64
Next, we are selecting multiple rows from the DataFrame using loc[] and iloc[]. Please note that when we select multiple rows from a DataFrame using loc[] and iloc[], the returned value is a DataFrame. And as we discussed, loc[] selects rows using their labels, where iloc[] selects rows using their indexes.
df2 = df1.loc[["Row 1", "Row 2"]] df3 = df1.iloc[[0, 1]] print("df2: \n", df2) print("df3: \n", df3)
The output of the above part of the program will be:
df2: Column 1 Column 2 Column 3 Row 1 1 2 3 Row 2 4 5 6 df3: Column 1 Column 2 Column 3 Row 1 1 2 3 Row 2 4 5 6
So, the complete output of the given program will be:






0 Comments