Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Renaming the Column | Preprocessing Data
Advanced Techniques in pandas
course content

Kursinnhold

Advanced Techniques in pandas

Advanced Techniques in pandas

1. Getting Familiar With Indexing and Selecting Data
2. Dealing With Conditions
3. Extracting Data
4. Aggregating Data
5. Preprocessing Data

book
Renaming the Column

In the previous chapter, you dealt with the incorrect values in a column. It is an excellent time to fix any changes. In our case, we can rename the column to pin that it was changed.

To rename a column, use the .rename() method. Look at the example where we will rename the column 'Survived', and then output the column names.

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/titanic4.csv', index_col = 0) data.rename(columns = {'Survived': 'Survived_Passenger'}, inplace = True) print(data.columns)
copy

Explanation:

python
  • .rename() - a method that we apply to the dataset to rename columns name;
  • columns = {'Survived': 'Survived_Passenger'} - in the curly brackets, you specify all columns and their new names. In this case, we renamed just one column, but you can put several of them here separated by commas.

.columns - an attribute that outputs the column names. In our case, we can no longer see the column name 'Survived'.

Oppgave

Swipe to start coding

Your task here is to:

  1. Rename the column 'Fare' to 'Fare_fixed'. Use the inplace = True argument.
  2. Output all column names of the data dataset.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 9
toggle bottom row

book
Renaming the Column

In the previous chapter, you dealt with the incorrect values in a column. It is an excellent time to fix any changes. In our case, we can rename the column to pin that it was changed.

To rename a column, use the .rename() method. Look at the example where we will rename the column 'Survived', and then output the column names.

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/titanic4.csv', index_col = 0) data.rename(columns = {'Survived': 'Survived_Passenger'}, inplace = True) print(data.columns)
copy

Explanation:

python
  • .rename() - a method that we apply to the dataset to rename columns name;
  • columns = {'Survived': 'Survived_Passenger'} - in the curly brackets, you specify all columns and their new names. In this case, we renamed just one column, but you can put several of them here separated by commas.

.columns - an attribute that outputs the column names. In our case, we can no longer see the column name 'Survived'.

Oppgave

Swipe to start coding

Your task here is to:

  1. Rename the column 'Fare' to 'Fare_fixed'. Use the inplace = True argument.
  2. Output all column names of the data dataset.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 9
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt