📜  在 Pandas Dataframe 中的给定位置插入行

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

在 Pandas Dataframe 中的给定位置插入行

在 Pandas DataFrame 中插入一行是一个非常简单的过程,我们已经讨论了如何在 Dataframe 开头插入行的方法。现在,让我们讨论在具有基于整数的索引的数据帧中的任何位置插入一行的方法。
解决方案#1: pandas 中不存在任何内置函数,这将帮助我们在给定数据帧的任何特定位置插入一行。因此,我们将编写自己的自定义函数来实现结果。
注意:在 Pandas Dataframe 中的行之间插入行是一种低效的操作,用户应该避免这样做。

Python3
# importing pandas as pd
import pandas as pd
 
# Let's create the dataframe
df = pd.DataFrame({'Date':['10/2/2011', '12/2/2011', '13/2/2011', '14/2/2011'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
 
# Let's visualize the dataframe
print(df)


Python3
# Function to insert row in the dataframe
def Insert_row(row_number, df, row_value):
    # Starting value of upper half
    start_upper = 0
  
    # End value of upper half
    end_upper = row_number
  
    # Start value of lower half
    start_lower = row_number
  
    # End value of lower half
    end_lower = df.shape[0]
  
    # Create a list of upper_half index
    upper_half = [*range(start_upper, end_upper, 1)]
  
    # Create a list of lower_half index
    lower_half = [*range(start_lower, end_lower, 1)]
  
    # Increment the value of lower half by 1
    lower_half = [x.__add__(1) for x in lower_half]
  
    # Combine the two lists
    index_ = upper_half + lower_half
  
    # Update the index of the dataframe
    df.index = index_
  
    # Insert a row at the end
    df.loc[row_number] = row_value
   
    # Sort the index labels
    df = df.sort_index()
  
    # return the dataframe
    return df
  
# Let's create a row which we want to insert
row_number = 2
row_value = ['11/2/2011', 'Wrestling', 12000]
 
if row_number > df.index.max()+1:
    print("Invalid row_number")
else:
     
    # Let's call the function and insert the row
    # at the second position
    df = Insert_row(row_number, df, row_value)
  
    # Print the updated dataframe
    print(df)


Python3
# importing pandas as pd
import pandas as pd
 
# Let's create the dataframe
df = pd.DataFrame({'Date':['10/2/2011', '12/2/2011', '13/2/2011', '14/2/2011'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
 
# Let's visualize the dataframe
print(df)


Python3
# Function to insert row in the dataframe
def Insert_row_(row_number, df, row_value):
    # Slice the upper half of the dataframe
    df1 = df[0:row_number]
  
    # Store the result of lower half of the dataframe
    df2 = df[row_number:]
  
    # Insert the row in the upper half dataframe
    df1.loc[row_number]=row_value
  
    # Concat the two dataframes
    df_result = pd.concat([df1, df2])
  
    # Reassign the index labels
    df_result.index = [*range(df_result.shape[0])]
  
    # Return the updated dataframe
    return df_result
  
# Let's create a row which we want to insert
row_number = 2
row_value = ['11/2/2011', 'Wrestling', 12000]
 
if row_number > df.index.max()+1:
    print("Invalid row_number")
else:
 
    # Let's call the function and insert the row
    # at the second position
    df = Insert_row_(2, df, row_value)
 
    # Print the updated dataframe
    print(df)


输出 :

现在我们将编写一个自定义函数来在数据框中的任何给定位置插入一行。

Python3

# Function to insert row in the dataframe
def Insert_row(row_number, df, row_value):
    # Starting value of upper half
    start_upper = 0
  
    # End value of upper half
    end_upper = row_number
  
    # Start value of lower half
    start_lower = row_number
  
    # End value of lower half
    end_lower = df.shape[0]
  
    # Create a list of upper_half index
    upper_half = [*range(start_upper, end_upper, 1)]
  
    # Create a list of lower_half index
    lower_half = [*range(start_lower, end_lower, 1)]
  
    # Increment the value of lower half by 1
    lower_half = [x.__add__(1) for x in lower_half]
  
    # Combine the two lists
    index_ = upper_half + lower_half
  
    # Update the index of the dataframe
    df.index = index_
  
    # Insert a row at the end
    df.loc[row_number] = row_value
   
    # Sort the index labels
    df = df.sort_index()
  
    # return the dataframe
    return df
  
# Let's create a row which we want to insert
row_number = 2
row_value = ['11/2/2011', 'Wrestling', 12000]
 
if row_number > df.index.max()+1:
    print("Invalid row_number")
else:
     
    # Let's call the function and insert the row
    # at the second position
    df = Insert_row(row_number, df, row_value)
  
    # Print the updated dataframe
    print(df)

输出 :

如果给定的 row_number 无效,例如数据帧中的总行数为 100,则 row_number 的最大值可以为 101,即在数据帧的最后添加行。任何大于 101 的数字都会给出错误消息。示例 #2:另一个自定义函数,它将使用 Pandas.concat()函数在数据帧的任何给定位置插入一行。

Python3

# importing pandas as pd
import pandas as pd
 
# Let's create the dataframe
df = pd.DataFrame({'Date':['10/2/2011', '12/2/2011', '13/2/2011', '14/2/2011'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
 
# Let's visualize the dataframe
print(df)

输出 :

一个自定义函数,用于在数据框中的任何给定位置插入一行。

Python3

# Function to insert row in the dataframe
def Insert_row_(row_number, df, row_value):
    # Slice the upper half of the dataframe
    df1 = df[0:row_number]
  
    # Store the result of lower half of the dataframe
    df2 = df[row_number:]
  
    # Insert the row in the upper half dataframe
    df1.loc[row_number]=row_value
  
    # Concat the two dataframes
    df_result = pd.concat([df1, df2])
  
    # Reassign the index labels
    df_result.index = [*range(df_result.shape[0])]
  
    # Return the updated dataframe
    return df_result
  
# Let's create a row which we want to insert
row_number = 2
row_value = ['11/2/2011', 'Wrestling', 12000]
 
if row_number > df.index.max()+1:
    print("Invalid row_number")
else:
 
    # Let's call the function and insert the row
    # at the second position
    df = Insert_row_(2, df, row_value)
 
    # Print the updated dataframe
    print(df)

输出 :