Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Adding a New Column | Section
Data Manipulation with pandas

bookAdding a New Column

Scorri per mostrare il menu

You have learned how to create a DataFrame. Next, explore what you can do with it. First, create a compact DataFrame with 3 columns and 7 rows.

1234567
import pandas as pd countries_data = {'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(countries_data) print(countries)
copy

You can expand the DataFrame by adding new columns using the following syntax:

dataframe['name_of_new_column'] = [value_1, value_2, value_3]
  • dataframe is the existing DataFrame you are adding a column to;
  • name_of_new_column is the name of the new column;
  • value_1, value_2, value_3 are the values that fill the new column.
Note
Note

The name of the new column should be enclosed in quotation marks and wrapped in square brackets, such as ['NewColumnName']. The values assigned to the new column should also be within square brackets, for example, data['NewColumnName'] = [value1, value2, value3]. If the values are numeric, they can be written without quotes, like [1, 2, 3]. If the values are strings, each one should be enclosed in quotes, like ['A', 'B', 'C'].

Next, add a 'population' column to the existing countries DataFrame.

12345678
import pandas as pd countries_data = {'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(countries_data) countries['population'] = [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200] print(countries)
copy

You can also use dot notation (e.g., df.column) for accessing existing columns, but it cannot be used to create new columns. Always use square brackets (e.g., df['column']) for this purpose.

12345678
import pandas as pd countries_data = {'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(countries_data) countries.population = [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200] print(countries)
copy

As expected, the 'population' column was not created since Pandas doesn't allow columns to be created using this approach.

question mark

Which of the following is the correct way to add a new column called 'population' with the given values to the countries DataFrame?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Sezione 1. Capitolo 5
some-alt