根据特定条件在 DataFrame 中插入行

Insert rows within a DataFrame, based on certain conditions

我有一个数据框,'df' 我想根据 df 本身的条件插入行。每个 'ID' 值都必须有一个对应于两种饮品类型(啤酒和葡萄酒)的条目。我想说的是,如果任何 ID 条目没有啤酒类型,例如,添加 DrinkType 等于 Beer 和 Drink 等于 'Not Stated' 的行。类似地,如果 ID 值指定了 Beer 而不是 Wine,则在 Drink Type 和 Drink 下添加 Wine 等于 'Not Stated' 的行。我希望 df 看起来像 df1。

df:    

ID    DrinkType    Drink

130   Beer         Fosters
130   Wine         Rose
130   Beer         Budweiser 
102   Beer         Fosters
120   Wine         Pinot Grigot
120   Beer         Budweiser 
99    Wine         Coke
75    Beer         Carling
75    Beer         Fosters


df1:    

ID    DrinkType    Drink

130   Beer         Fosters
130   Wine         Rose
130   Beer         Budweiser 
102   Beer         Fosters
102   Wine         Not Stated
120   Wine         Pinot Grigot
120   Beer         Budweiser 
99    Wine         Coke   
99    Beer         Not Stated
75    Beer         Carling
75    Beer         Fosters
75    Wine         Not Stated

在特定索引处的数据框中插入行的函数

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:] 

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

让我们创建一个要插入的行

index = 2
item_insert = ['Beer','NotStated'] 

if row_number > df.index.max()+1: 
    df[index]=row_value 
else: 
    # Let's call the function and insert the row 
    # at the second position 
    df = Insert_row_(2, df, row_value) 


print(df) 

最后插入任意行

找到当前数据帧的最大索引

df[s]=rowvalue or we can use pd.concat(df,df1)