简体   繁体   English

如何将数据框与列上的特定值一分为二

[英]how to split a dataframe in two from a specific value on a column

I have this dataframe: 我有这个数据框:

在此处输入图片说明

In row 84 it changes from a negative value in the 'position' column to positive. 在第84行中,它从“位置”列中的负值变为正值。

I need to split the data frame into two at this point (before and after position=0) 我需要在这一点上将数据帧分为两部分(在position = 0之前和之后)

I tried using 我尝试使用

idx = run_1[run_1['Position_(m)']>0].index dfs = np.split(df, idx)

but it split on every instance where >0 and if I use idx[0] it says: 但它会在> 0的每个实例上拆分,如果我使用idx [0],它会说:

ValueError: array split does not result in an equal division ValueError:数组拆分不会导致均等

Note: I only need one split, "before and after," I have looked through other similar questions to mine but most deal with multiple splits and I'm unable to re-use the code sugested for a single split. 注意:我只需要一个拆分(“之前和之后”),就可以找到其他类似的问题,但是大多数问题涉及多个拆分,因此我无法重用针对单个拆分所引用的代码。

You can use a Boolean mask to do this: 您可以使用布尔掩码来执行此操作:

before = run_9[run_9['Position_(m)']<0]
after = run_9[run_9['Position_(m)']>0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM