在Pandas中为现有的DataFrame添加新列
让我们讨论一下如何在Pandas中为现有的DataFrame添加新的列。我们有多种方法可以完成这项任务。
方法一:通过声明一个新的列表作为列。
# 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
print(df)
输出:
注意,你的列表的长度应该与索引列的长度一致,否则会显示错误。
方法#2:通过使用DataFrame.insert()。
它让我们可以自由地在任何位置添加一列,而不仅仅是在最后。它还提供了插入列值的不同选项。
示例
# 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
print(df)
输出:
方法#3:使用Dataframe.assign()方法
这个方法将创建一个新的数据框架,并在旧的数据框架中添加一个新的列。
示例
# 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
print(df2)
输出:
方法四:通过使用字典。
我们可以使用Python字典在pandas DataFrame中添加一个新列。使用一个现有的列作为键值,它们各自的值将成为新列的值。
示例
# 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
print(df)
输出: