在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 pandasimport 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 Kareem 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 pandasimport 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', data)
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 Name Pythoneer
1 Aslan 18 Age Programmer
2 Kareem 15 Profession 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 pandasimport 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
结论
以上是 在Python中的Pandas中向现有DataFrame添加新列 的全部内容, 来源链接: utcz.com/z/354353.html