Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Indexes and Columns | Basics
Introduction to pandas [track]
course content

Cursusinhoud

Introduction to pandas [track]

Introduction to pandas [track]

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

book
Indexes and Columns

You can change DataFrame' indexes likewise Series' indexes (by assigning necessary list to the .index attribute of DataFrame).

12345678
# Importing library import pandas as pd # Creating DataFrame df = pd.DataFrame([[1, 2], [3, 4], [5, 6]]) # Set indexes df.index = ['first', 'second', 'third'] print(df)
copy

You can set indexes when defining the DataFrame by specifying the index parameter. For instance,

123456
# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], index = ['first', 'second', 'third']) print(df)
copy

Also there are two ways to set the columns names: by assigning list of names to the columns attribute, or specifying the columns parameter.

12345678910
# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], columns = ['col1', 'col2'], index = ['first', 'second', 'third']) print(df) # Changing columns' names df.columns = ['Column1', 'Column2'] print(df)
copy

If you need to change columns' names or indexes of existing dataframe, use the approach with reassigning (.index, .columns attributes).

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 6

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

course content

Cursusinhoud

Introduction to pandas [track]

Introduction to pandas [track]

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

book
Indexes and Columns

You can change DataFrame' indexes likewise Series' indexes (by assigning necessary list to the .index attribute of DataFrame).

12345678
# Importing library import pandas as pd # Creating DataFrame df = pd.DataFrame([[1, 2], [3, 4], [5, 6]]) # Set indexes df.index = ['first', 'second', 'third'] print(df)
copy

You can set indexes when defining the DataFrame by specifying the index parameter. For instance,

123456
# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], index = ['first', 'second', 'third']) print(df)
copy

Also there are two ways to set the columns names: by assigning list of names to the columns attribute, or specifying the columns parameter.

12345678910
# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], columns = ['col1', 'col2'], index = ['first', 'second', 'third']) print(df) # Changing columns' names df.columns = ['Column1', 'Column2'] print(df)
copy

If you need to change columns' names or indexes of existing dataframe, use the approach with reassigning (.index, .columns attributes).

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 6
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt