Adding a New Column
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.
1234567import 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)
You can expand the DataFrame by adding new columns using the following syntax:
dataframe['name_of_new_column'] = [value_1, value_2, value_3]
dataframeis the existingDataFrameyou are adding a column to;name_of_new_columnis the name of the new column;value_1, value_2, value_3are the values that fill the new column.
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.
12345678import 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)
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.
12345678import 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)
As expected, the 'population' column was not created since Pandas doesn't allow columns to be created using this approach.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Why can't I use dot notation to create new columns in a DataFrame?
Can you explain the difference between dot notation and square bracket notation in pandas?
What happens if I try to use dot notation to create a new column?
Awesome!
Completion rate improved to 3.03
Adding a New Column
Swipe to show 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.
1234567import 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)
You can expand the DataFrame by adding new columns using the following syntax:
dataframe['name_of_new_column'] = [value_1, value_2, value_3]
dataframeis the existingDataFrameyou are adding a column to;name_of_new_columnis the name of the new column;value_1, value_2, value_3are the values that fill the new column.
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.
12345678import 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)
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.
12345678import 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)
As expected, the 'population' column was not created since Pandas doesn't allow columns to be created using this approach.
Thanks for your feedback!