在特定条件下如何在数据框中插入一行? [英] How to insert a row in a data frame under specific conditions?

查看:144
本文介绍了在特定条件下如何在数据框中插入一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解pandas旨在加载完全填充的数据框,但是我想在特定条件下向数据框添加单行.以下是我正在处理的df

I understand that pandas is designed to load completely filled dataframes, but I want to add single rows to to the data frame under specific condition. Following is the df I am dealing with

       ProjID      Xcoord      Ycoord
    0  2           some_val    some_val
    1  2           some_val    some_val
    2  2           some_val    some_val
    3  2           some_val    some_val
    4  2           some_val    some_val
    5  3           some_val    some_val
    6  3           some_val    some_val
    7  5           some_val    some_val
    8  5           some_val    some_val
    9  5           some_val    some_val

我想要的是每当ProjID更改时,在df中插入一行,每列的值为0.以下是必需的df:

What I want is to insert a row in the df with value 0 for every column, whenever the ProjID changes. Below is the required df:

      ProjID      Xcoord      Ycoord
    0  2           some_val    some_val
    1  2           some_val    some_val
    2  2           some_val    some_val
    3  2           some_val    some_val
    4  2           some_val    some_val
    5  0           0           0 
    6  3           some_val    some_val
    7  3           some_val    some_val
    8  0           0           0 
    9  5           some_val    some_val
   10  5           some_val    some_val
   11  5           some_val    some_val
   12  0           0           0

基本上,每次插入0值的行时,ProjID都会更改. 我试图编写一个for循环,其中ProjID col中的值与ProjID col的前一行值进行检查,如果它们相同,则它将继续前进,如果它们不相同,则应插入包含所有内容的行0个值.但是,我无法实现它.另外,我也不知道索引列的行为.

Basically a row with 0 values is inserted everytime, the ProjID changes. I was trying to write a for loop where the values in ProjID col is checked with previous row value of the ProjID col, if they are same, then it will move ahead, if they are not same, then it should insert a row with all 0 values. But, I am unable to implement it. Also, I have no idea how the index column will behave.

请让我知道在df中是否可以进行这种行插入,以及如何执行.感谢您的帮助.

Please let me know if this kind of insertion of a row is possible in a df, and how do I do it. Thanks for any help.

推荐答案

您可以按 ProjID 对数据帧进行分组,然后使用append方法在行的每个子数据帧中插入一行.最后,这假定所有相同的ProjID都已在手之前进行了排序:

You can group data frame by ProjID, and use the append method to insert a row to each sub data frame at the end, this assumes all same ProjID have been sorted together before hand:

(df.groupby("ProjID", group_keys=False)
   .apply(lambda g: g.append(pd.Series(0, g.columns), ignore_index=True))
   .reset_index(drop=True))

这篇关于在特定条件下如何在数据框中插入一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆