1

I have a simple dataframe in which I am trying to split into multiple groups based on whether the x column value falls within a range.

e.g. if I have:

print(df1)
  x
0 5
1 7.5
2 10
3 12.5
4 15

And wish to create a new dataframe, df2, of values of x which are within the range 7-13 (7 < x < 13)

print(df1)
  x   
0 5
4 15

print(df2)
  x
1 7.5
2 10
3 12.5

I have been able to split the dataframe based on a single value boolean e.g. ( x < 11), using the following - but have unable to develop this into a range of values.

thresh = 11
df2 = df1[df1['x'] < thresh]

print(df2)
  x
0 5
1 7.5
2 10

2 Answers 2

3

You can create a boolean mask for the range (7 < x < 13) by AND condition of (x > 7) and (x < 13). Then create df2 with this boolean mask. The remaining entries left in df1 being the negation of this boolean mask:

thresh_low = 7
thresh_high = 13
mask = (df1['x'] > thresh_low) & (df1['x'] < thresh_high)

df2 = df1[mask]
df1 = df1[~mask]

Result:

print(df2)

      x
1   7.5
2  10.0
3  12.5


print(df1)

      x
0   5.0
4  15.0

Sign up to request clarification or add additional context in comments.

Comments

1

You can use between to categorize whether the condition is met and then groupby to split based on your condition. Here I'll store the results in a dict

d = dict(tuple(df1.groupby(df1['x'].between(7, 13, inclusive=False))))

d[True]
#      x
#1   7.5
#2  10.0
#3  12.5

d[False]
#      x
#0   5.0
#4  15.0

Or with only two possible splits you can manually define the Boolean Series and then split based on it.

m = df1['x'].between(7, 13, inclusive=False)

df_in = df1[m]
df_out = df1[~m]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.