Conteúdo do Curso
Pandas First Steps
Pandas First Steps
Adding a New Column
We've learned how to create a DataFrame. Now let's explore what we can do with it.
First, let's craft a compact DataFrame consisting of three columns and seven rows.
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) print(countries)
You can expand the DataFrame by adding new columns, and there are multiple ways to do it. We'll focus on two methods. The syntax for the first method is as follows:
dataframe
is the name of our existing DataFrame to which we'll add new columns;name_of_new_column
is the name you're giving to the new column you're adding.;value_1, value_2, value_3
are the values that will populate the new column.
Note
The name of the new column should be enclosed in quotation marks and wrapped in square brackets. Likewise, the values inserted into the new column should also be within square brackets. If the values are numeric, they don't need to be in quotes; if they're strings, then quotes are necessary.
Now, let's demonstrate how to add a population
column to our pre-existing countries
DataFrame.
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['population'] = [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200] print(countries)
Note
Using this method, the new column will be appended to the end of the DataFrame.
Obrigado pelo seu feedback!