Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Inserting a New Column | The Very First Steps
Pandas First Steps
course content

Conteúdo do Curso

Pandas First Steps

Pandas First Steps

1. The Very First Steps
2. Reading Files in Pandas
3. Analyzing the Data

Inserting a New Column

Let's explore the second technique for adding a column to a DataFrame. This approach utilizes the insert() method. Here's the syntax:

  • dataframe: The name of the existing DataFrame;
  • insert(): The method used for adding new columns;
  • column_index: The position where the new column will be inserted (keep in mind that indexing starts at 0);
  • column_name: The name for the new column;
  • [value_1, value_2, value_3]: The values that will populate the new column.

Now let's turn our attention to the countries DataFrame. We'll demonstrate how to add a new column named population, representing the populations of countries, right after the first column (country).

12345678
import pandas as pd dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(dataset) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
copy

Note

A key benefit of the insert() method is that it allows you to specify the position of the new column within the DataFrame.

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 8
toggle bottom row

Inserting a New Column

Let's explore the second technique for adding a column to a DataFrame. This approach utilizes the insert() method. Here's the syntax:

  • dataframe: The name of the existing DataFrame;
  • insert(): The method used for adding new columns;
  • column_index: The position where the new column will be inserted (keep in mind that indexing starts at 0);
  • column_name: The name for the new column;
  • [value_1, value_2, value_3]: The values that will populate the new column.

Now let's turn our attention to the countries DataFrame. We'll demonstrate how to add a new column named population, representing the populations of countries, right after the first column (country).

12345678
import pandas as pd dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(dataset) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
copy

Note

A key benefit of the insert() method is that it allows you to specify the position of the new column within the DataFrame.

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 8
toggle bottom row

Inserting a New Column

Let's explore the second technique for adding a column to a DataFrame. This approach utilizes the insert() method. Here's the syntax:

  • dataframe: The name of the existing DataFrame;
  • insert(): The method used for adding new columns;
  • column_index: The position where the new column will be inserted (keep in mind that indexing starts at 0);
  • column_name: The name for the new column;
  • [value_1, value_2, value_3]: The values that will populate the new column.

Now let's turn our attention to the countries DataFrame. We'll demonstrate how to add a new column named population, representing the populations of countries, right after the first column (country).

12345678
import pandas as pd dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(dataset) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
copy

Note

A key benefit of the insert() method is that it allows you to specify the position of the new column within the DataFrame.

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Let's explore the second technique for adding a column to a DataFrame. This approach utilizes the insert() method. Here's the syntax:

  • dataframe: The name of the existing DataFrame;
  • insert(): The method used for adding new columns;
  • column_index: The position where the new column will be inserted (keep in mind that indexing starts at 0);
  • column_name: The name for the new column;
  • [value_1, value_2, value_3]: The values that will populate the new column.

Now let's turn our attention to the countries DataFrame. We'll demonstrate how to add a new column named population, representing the populations of countries, right after the first column (country).

12345678
import pandas as pd dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(dataset) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
copy

Note

A key benefit of the insert() method is that it allows you to specify the position of the new column within the DataFrame.

Tarefa

  1. Using the dataset data, create the audi_cars DataFrame.
  2. Insert a column named 'price' between the year and fueltype columns. Use the .insert() method and populate it with the following values: [12500, 16500, 16800, 17300, 13900].

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 8
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt