Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Accessing Specific Columns | 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 Specific Columns

How to get a certain column in a DataFrame? Put column name (as string, in quotes) between square brackets and place it right to variable with dataframe. Another way is to refer to column like to attribute - put column name (without quotes) right to dot symbol placed right to variable with dataframe (it works only for column that has only one word in name).

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 'year' column print(df['year']) # Get 'model' column print(df.model)
copy

If you want to get several columns at once, you need to pass list of columns you want to get. The second approach described above doesn't work here.

1234567
# 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 columns 'model' and 'year' print(df[['model', 'year']])
copy

Pay attention to square brackets. Syntax df(['model', 'year']) will not work!

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

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 Specific Columns

How to get a certain column in a DataFrame? Put column name (as string, in quotes) between square brackets and place it right to variable with dataframe. Another way is to refer to column like to attribute - put column name (without quotes) right to dot symbol placed right to variable with dataframe (it works only for column that has only one word in name).

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 'year' column print(df['year']) # Get 'model' column print(df.model)
copy

If you want to get several columns at once, you need to pass list of columns you want to get. The second approach described above doesn't work here.

1234567
# 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 columns 'model' and 'year' print(df[['model', 'year']])
copy

Pay attention to square brackets. Syntax df(['model', 'year']) will not work!

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

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