Andrea
Andrea

Reputation: 748

Splitting dataframes on range of values in one column

Say I have the following dataframe:

df = pd.DataFrame({'A' : [0, 0.3, 0.8, 1, 1.5, 2.3, 2.3, 2.9], 'B' : randn(8)})

df
Out[86]: 
    A         B
0  0.0  0.130471
1  0.3  0.029251
2  0.8  0.790972
3  1.0 -0.870462
4  1.5 -0.700132
5  2.3 -0.361464
6  2.3 -1.100923
7  2.9 -1.003341

How could I split this dataframe based on the range of Col A values as in the following (0<=A<1, 1<=A<2, etc.)?:

    A         B
0  0.0  0.130471
1  0.3  0.029251
2  0.8  0.790972

    A         B
3  1.0 -0.870462
4  1.5 -0.700132

    A         B
5  2.3 -0.361464
6  2.3 -1.100923
7  2.9 -1.003341

I know that np.array could be used if this were to be split based on equal number of rows:

np.array_split(df, 3)

but does something similar exist that allows me to apply my condition here?

Upvotes: 1

Views: 521

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150785

Try with groupby and floored division:

for k, d in df.groupby(df['A']//1):
    print(d)

Output:

     A         B
0  0.0  0.130471
1  0.3  0.029251
2  0.8  0.790972
     A         B
3  1.0 -0.870462
4  1.5 -0.700132
     A         B
5  2.3 -0.361464
6  2.3 -1.100923
7  2.9 -1.003341

Upvotes: 1

Related Questions