在 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
结论
如果您对本教程有任何疑问,请在评论区提出。
广告