在Python的Pandas中向现有的DataFrame中添加新列

在Python的Pandas中向现有的DataFrame中添加新列

在本教程中,我们将学习如何向现有的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

结论

如果您对教程有任何疑问,请在评论部分提及。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程