📌  相关文章
📜  在 Pandas 中向现有 DataFrame 添加新列

📅  最后修改于: 2022-05-13 01:55:32.720000             🧑  作者: Mango

在 Pandas 中向现有 DataFrame 添加新列

让我们讨论如何在 Pandas 中向现有 DataFrame 添加新列。我们可以通过多种方式完成这项任务。

方法 #1:通过将新列表声明为列。

Python3
# Import pandas package
import pandas as pd
 
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Declare a list that is to be converted into a column
address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
 
# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address
 
# Observe the result
df


Python3
# Import pandas package
import pandas as pd
 
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Using DataFrame.insert() to add a column
df.insert(2, "Age", [21, 23, 24, 21], True)
 
# Observe the result
df


Python3
# Import pandas package
import pandas as pd
  
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
  
  
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Using 'Address' as the column name and equating it to the list
df2 = df.assign(address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'])
  
# Observe the result
df2


Python3
# Import pandas package
import pandas as pd
 
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
 
# Define a dictionary with key values of
# an existing column and their respective
# value pairs as the # values for our new column.
address = {'Delhi': 'Jai', 'Bangalore': 'Princi',
           'Patna': 'Gaurav', 'Chennai': 'Anuj'}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Provide 'Address' as the column name
df['Address'] = address
 
# Observe the output
df


输出:

请注意,列表的长度应与索引列的长度匹配,否则会显示错误。

方法 #2:通过使用 DataFrame.insert()
它提供了在我们喜欢的任何位置添加列的自由,而不仅仅是在末尾。它还提供了用于插入列值的不同选项。

Python3

# Import pandas package
import pandas as pd
 
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Using DataFrame.insert() to add a column
df.insert(2, "Age", [21, 23, 24, 21], True)
 
# Observe the result
df

输出:

方法 #3:使用 Dataframe.assign() 方法
此方法将创建一个新数据框,并将新列添加到旧数据框中。

Python3

# Import pandas package
import pandas as pd
  
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
  
  
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Using 'Address' as the column name and equating it to the list
df2 = df.assign(address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'])
  
# Observe the result
df2

输出:

方法#4:使用字典
我们可以使用Python字典在 pandas DataFrame 中添加一个新列。使用现有列作为键值,它们各自的值将成为新列的值。

Python3

# Import pandas package
import pandas as pd
 
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
 
# Define a dictionary with key values of
# an existing column and their respective
# value pairs as the # values for our new column.
address = {'Delhi': 'Jai', 'Bangalore': 'Princi',
           'Patna': 'Gaurav', 'Chennai': 'Anuj'}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Provide 'Address' as the column name
df['Address'] = address
 
# Observe the output
df

输出: