简体   繁体   中英

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.

I need to split the data frame into two at this point (before and after 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:

ValueError: array split does not result in an equal division

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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