Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Adding a New Column | The Very First Steps
Introduction to Pandas

bookAdding 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.

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.

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  7
some-alt