在 Python 的 Pandas 中向現有 DataFrame 新增新列
在本教程中,我們將學習如何向現有的Pandas DataFrame新增新列。我們可以用不同的方法新增新列。讓我們一起來看看。
使用列表
我們可以使用列表新增新列。請按照以下步驟新增新列。
演算法
1. Create DataFrame using a dictionary. 2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame. 3. Add the list to the DataFrame like dictionary element.
讓我們看一個例子。
示例
# importing pandas import pandas as pd # creating a DataFrame data = { 'Name': ['Hafeez', 'Aslan', 'Kareem'], 'Age': [19, 18, 15], 'Profession': ['Pythoneer', 'Programmer', 'Student'] } dataframe = pd.DataFrame(data) print('-----------Before adding a new column----------') print(dataframe) print('\n\n') # creating a list for new column places = ['Nellore', 'Mumbai', 'Andhra'] # we are using 'Places' as column name # adding the list to the dataframe as column dataframe['Places'] = places print('---------------After adding a new column------------') print(dataframe)
輸出
執行上述程式,您將獲得以下結果。
-----------Before adding a new column---------- Name Age Profession 0 Hafeez 19 Pythoneer 1 Aslan 18 Programmer 2 Kareem 15 Student ---------------After adding a new column------------ Name Age Profession Places 0 Hafeez 19 Pythoneer Nellore 1 Aslan 18 Programmer Mumbai 2 K areem 15 Student Andhra
DataFrame.insert()
有一個名為insert()的內建方法可以新增新列。操作步驟如下。
演算法
1. Create DataFrame using dictionary. 2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame. 3. Insert the data into the DataFrame using DataFrame.insert(index, column_name, data) method.
示例
# importing pandas import pandas as pd # creating a DataFrame data = { 'Name': ['Hafeez', 'Aslan', 'Kareem'], 'Age': [19, 18, 15], 'Profession': ['Pythoneer', 'Programmer', 'Student'] } dataframe = pd.DataFrame(data) print('-----------Before adding a new column----------') print(dataframe) print('\n\n') # creating a list for new column places = ['Nellore', 'Mumbai', 'Andhra'] # we are using 'Places' as column name # adding the list to the dataframe as column using insert(index, column_name, data) dataframe.insert(2, 'Places', places) print('---------------After adding a new column------------') print(dataframe)
輸出
執行上述程式,您將獲得以下結果。
-----------Before adding a new column---------- Name Age Profession 0 Hafeez 19 Pythoneer 1 Aslan 18 Programmer 2 Kareem 15 Student ---------------After adding a new column------------ Name Age Places Profession 0 Hafeez 19 Nellore Pythoneer 1 Aslan 18 Mumbai Programmer 2 Kareem 15 Andhra Student
DataFrame.assign()
此方法接受一個引數,即資料列表,並將其作為列新增到資料框的末尾。
演算法
1. Create DataFrame using a dictionary. 2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame. 3. Insert the data into the DataFrame using DataFrame.assign(column_name = data) method. It returns a new data frame. So, we have to store it. 4. Print the new data frame.
讓我們看一個例子。
示例
# importing pandas import pandas as pd # creating a DataFrame data = { 'Name': ['Hafeez', 'Aslan', 'Kareem'], 'Age': [19, 18, 15], 'Profession': ['Pythoneer', 'Programmer', 'Student'] } dataframe = pd.DataFrame(data) print('-----------Before adding a new column----------') print(dataframe) print('\n\n') # creating a list for new column places = ['Nellore', 'Mumbai', 'Andhra'] # we are using 'Places' as column name # adding the list to the dataframe as column using assign(column_name = data) new_dataframe = dataframe.assign(Places = places) print('---------------After adding a new column------------') print(new_dataframe)
輸出
執行上述程式,您將獲得以下結果。
-----------Before adding a new column---------- Name Age Profession 0 Hafeez 19 Pythoneer 1 Aslan 18 Programmer 2 Kareem 15 Student ---------------After adding a new column------------ Name Age Profession Places 0 Hafeez 19 Pythoneer Nellore 1 Aslan 18 Programmer Mumbai 2 Kareem 15 Student Andhra
結論
如果您對本教程有任何疑問,請在評論區提出。
廣告