📌  相关文章
📜  在 Pandas DataFrame 中的特定位置插入给定列

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

在 Pandas DataFrame 中的特定位置插入给定列

在本文中,我们将使用 Pandas 的Dataframe.insert()方法在数据框中的特定列索引处插入新列。

代码:让我们创建一个数据框。

Python3
# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10],
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# show the dataframe
df


Python3
# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10], 
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# New column to be added
new_col = [1, 2, 3, 4, 5] 
  
# Inserting the column at the
# beginning in the DataFrame
df.insert(loc = 0,
          column = 'col1',
          value = new_col)
# show the dataframe
df


Python3
# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10], 
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# New column to be added
new_col = [1, 2, 3, 4, 5] 
  
# Inserting the column at the
# middle of the DataFrame
df.insert(loc = 1,
          column = 'col1',
          value = new_col)
# show the dataframe
df


Python3
# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10], 
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# New column to be added
new_col = [1, 2, 3, 4, 5] 
  
# Inserting the column at the
# end of the DataFrame
# df.columns gives index array 
# of column names
df.insert(loc = len(df.columns),
          column = 'col1',
          value = new_col)
# show the dataframe
df


输出:

数据框

示例 1:在数据框的开头插入列。

Python3

# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10], 
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# New column to be added
new_col = [1, 2, 3, 4, 5] 
  
# Inserting the column at the
# beginning in the DataFrame
df.insert(loc = 0,
          column = 'col1',
          value = new_col)
# show the dataframe
df

输出:

在数据框的开头插入新列

示例 2:在数据框中间插入列

Python3

# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10], 
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# New column to be added
new_col = [1, 2, 3, 4, 5] 
  
# Inserting the column at the
# middle of the DataFrame
df.insert(loc = 1,
          column = 'col1',
          value = new_col)
# show the dataframe
df

输出:

在数据框中间插入新列

示例 3:在数据框末尾插入列

Python3

# Importing pandas library
import pandas as pd
  
# dictionary
values = {'col2': [6, 7, 8, 
                   9, 10], 
          'col3': [11, 12, 13,
                   14, 15]}
  
# Creating dataframe
df = pd.DataFrame(values)
  
# New column to be added
new_col = [1, 2, 3, 4, 5] 
  
# Inserting the column at the
# end of the DataFrame
# df.columns gives index array 
# of column names
df.insert(loc = len(df.columns),
          column = 'col1',
          value = new_col)
# show the dataframe
df

输出:

在数据框末尾插入新列