loc and iloc (3)
It's also possible to select only columns with loc and iloc. In both cases, you simply put a slice going from beginning to end in front of the comma:
cars.loc[:, 'country']
cars.iloc[:, 1]
cars.loc[:, ['country','drives_right']]
cars.iloc[:, [1, 2]]
Diese Übung ist Teil des Kurses
Intermediate Python
Anleitung zur Übung
- Print out the
drives_rightcolumn as a Series usinglocoriloc. - Print out the
drives_rightcolumn as a DataFrame usinglocoriloc. - Print out both the
cars_per_capanddrives_rightcolumn as a DataFrame usinglocoriloc.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)
# Print out drives_right column as Series
# Print out drives_right column as DataFrame
# Print out cars_per_cap and drives_right as DataFrame