Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Accessing using .loc[] [2/3] | Accessing DataFrame Values
Introduction to pandas [track]
course content

Kursusindhold

Introduction to pandas [track]

Introduction to pandas [track]

1. Basics
2. Reading and Exploring Data
3. Accessing DataFrame Values
4. Aggregate Functions

book
Accessing using .loc[] [2/3]

Not only you can get certain rows, but you can also get specific columns. To set column/columns you want to receive, pass it as the second parameter between square brackets of .loc[].

If you want to indicate only one column, just place its name. If you want to indicate several columns, you need to pass list of their names.

Note that the first parameter must be set, even if you don't want to indicate specific rows (use : instead).

123456789
# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the first five rows (indexes 0-4) and column 'fuelType' print(df.loc[:4, 'fuelType']) # Get the 9th - 11th rows (indexes 8, 9, 10) and columns 'model', 'year', and 'price' print(df.loc[8:10, ['model', 'year', 'price']])
copy

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

Introduction to pandas [track]

Introduction to pandas [track]

1. Basics
2. Reading and Exploring Data
3. Accessing DataFrame Values
4. Aggregate Functions

book
Accessing using .loc[] [2/3]

Not only you can get certain rows, but you can also get specific columns. To set column/columns you want to receive, pass it as the second parameter between square brackets of .loc[].

If you want to indicate only one column, just place its name. If you want to indicate several columns, you need to pass list of their names.

Note that the first parameter must be set, even if you don't want to indicate specific rows (use : instead).

123456789
# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the first five rows (indexes 0-4) and column 'fuelType' print(df.loc[:4, 'fuelType']) # Get the 9th - 11th rows (indexes 8, 9, 10) and columns 'model', 'year', and 'price' print(df.loc[8:10, ['model', 'year', 'price']])
copy

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7
Vi beklager, at noget gik galt. Hvad skete der?
some-alt