Issue
I have a pandas dataframe tat looks something like this
A BB
1 foo.bar
2 foo.bar
3 foo.foo
4 foo.bar
5 foo.bar
6 foo.foo
I basically expect to get two dataframes out of them based on this list of lists:
[[False, False, True], [False, False, True]]
OUTPUT should be:
df1:
A BB
1 foo.bar
2 foo.bar
3 foo.foo
df2
A BB
4 foo.bar
5 foo.bar
6 foo.foo
Solution
Numpy:
flatnonzero
to find where the'foo.foo'
rows aresplit
to divide the dataframe up accordingly
import numpy as np
np.split(df, np.flatnonzero(df.BB.eq('foo.foo'))[:-1] + 1)
[ A BB
0 1 foo.bar
1 2 foo.bar
2 3 foo.foo,
A BB
3 4 foo.bar
4 5 foo.bar
5 6 foo.foo]
Addressing @mozway's comment
list(filter(
lambda d: not d.empty,
np.split(df, np.flatnonzero(df.BB.eq('foo.foo')) + 1)
))
[ A BB
0 1 foo.bar
1 2 foo.bar
2 3 foo.foo,
A BB
3 4 foo.bar
4 5 foo.bar
5 6 foo.foo]
Answered By - piRSquared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.