在Pandas数据框架的指定位置插入行

在Pandas数据框架的指定位置插入行

Pandas DataFrame中插入一行是一个非常直接的过程,我们已经讨论过如何在Dataframe的开始插入行的方法。现在,让我们来讨论一下如何在数据框架中的任何位置插入具有整数索引的行。
解决方案 #1 : 在pandas中不存在任何内置的函数,可以帮助我们在给定的数据框架中的任何特定位置插入一行。所以,我们要写自己的自定义函数来实现这个结果。
注意:Pandas Dataframe中的行之间插入行是一种低效的操作,用户应该避免这样做。

# 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)

输出 :

在Pandas数据框架的指定位置插入行

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

# 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)

输出 :

在Pandas数据框架的指定位置插入行

如果给定的row_number是无效的,比如说数据框架中的总行数是100,那么row_number的最大值可以是101,即在数据框架的最后加入行。任何大于101的数字将给出一个错误消息。

例子2:另一个自定义函数,它将使用Pandas.concat()函数在数据框架的任何给定位置插入一行。

# 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)

输出 :

在Pandas数据框架的指定位置插入行

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

# 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)

输出 :

在Pandas数据框架的指定位置插入行

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程