且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据列中的条件将Pandas数据框拆分为多个数据框

更新时间:2022-12-12 10:24:20

您可以使用 np.split ,它接受在哪里分割的索引数组:

You can use np.split which accepts an array of indices where to split:

np.split(df, *np.where(df.BOOL == 1))

如果要在前一个数据框中包含 BOOL == 1 的行,则只需添加1到所有索引:

If you want to include the rows with BOOL == 1 to the previous data frame you can just add 1 to all the indices:

np.split(df, np.where(df.BOOL == 1)[0] + 1)