在 Pandas 中向数据框添加多列
在 Pandas 中,我们可以在需要时自由地在数据框中添加列。有多种方法可以向 Pandas 数据框添加列。
方法 1:使用Lists将多个列添加到数据框中
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# Creating 2 lists 'marks' and 'gender'
marks = [85.4,94.9,55.2,100.0,40.5,33.5]
gender = ['M','F','M','F','F','M']
# adding lists as new column to dataframe df
df['Uni_Marks'] = marks
df['Gender'] = gender
# Displaying the Data frame
df
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# creating columns 'Admissionnum' and 'Percentage'
# using dataframe.assign() function
df = df.assign(Admissionnum=[250, 800, 1200, 300, 400, 700],
Percentage=['85%', '90%', '75%', '35%', '60%', '80%'])
# Displaying the Data frame
df
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# creating columns 'Age' and 'ID' at
# 2nd and 3rd position using
# dataframe.insert() function
df.insert(2, "Marks", [90, 70, 45, 33, 88, 77], True)
df.insert(3, "ID", [101, 201, 401, 303, 202, 111], True)
# Displaying the Data frame
df
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# creating 2 lists 'ids' and 'marks'
ids = [11, 12, 13, 14, 15, 16]
marks=[85,41,77,57,20,95,96]
# Creating columns 'ID' and 'Uni_marks'
# using Dictionary and zip()
df['ID'] = dict(zip(ids, df['Name']))
df['Uni_Marks'] = dict(zip(marks, df['Name']))
# Displaying the Data frame
df
输出 :
方法 2:使用Dataframe.assign()方法将多列添加到数据框中
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# creating columns 'Admissionnum' and 'Percentage'
# using dataframe.assign() function
df = df.assign(Admissionnum=[250, 800, 1200, 300, 400, 700],
Percentage=['85%', '90%', '75%', '35%', '60%', '80%'])
# Displaying the Data frame
df
输出 :
方法 3:使用Dataframe.insert()方法将多列添加到数据框中
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# creating columns 'Age' and 'ID' at
# 2nd and 3rd position using
# dataframe.insert() function
df.insert(2, "Marks", [90, 70, 45, 33, 88, 77], True)
df.insert(3, "ID", [101, 201, 401, 303, 202, 111], True)
# Displaying the Data frame
df
输出 :
方法 4:使用Dictionary和zip()将多列添加到数据框中
Python3
# importing pandas library
import pandas as pd
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
['Ritika', 30, 'Delhi', 'India'],
['Vansh', 31, 'Delhi', 'India'],
['Nany', 32, 'Tokyo', 'Japan'],
['May', 16, 'New York', 'US'],
['Michael', 17, 'las vegas', 'US']]
# Create a DataFrame object
df = pd.DataFrame(students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# creating 2 lists 'ids' and 'marks'
ids = [11, 12, 13, 14, 15, 16]
marks=[85,41,77,57,20,95,96]
# Creating columns 'ID' and 'Uni_marks'
# using Dictionary and zip()
df['ID'] = dict(zip(ids, df['Name']))
df['Uni_Marks'] = dict(zip(marks, df['Name']))
# Displaying the Data frame
df
输出 :