Reputation: 65
I need to create multiple dataframes from one dataframe; the requirement is to slice the dataframe by a known value and slice it. I haven't found something similar like it so far.
The toy code below builds a Pandas dataframe from my data logger:
import pandas as pd
my_key = 0.0
df = pd.DataFrame({'A': [1.0, 2.0, 3.0, my_key, my_key, my_key, 5.0, 6.0, 7.0, my_key, my_key, my_key, 10.0, 11.0, 12.0],
'B': [21.0, 22.0, 23.0, 23.1, 23.2, 23.3, 25.0, 26.0, 27.0, 27.1, 27.2, 27.3, 30.0, 31.0, 32.0]})
The code generates this data:
A B
0 1.0 21.0
1 2.0 22.0
2 3.0 23.0
3 0.0 23.1
4 0.0 23.2
5 0.0 23.3
6 5.0 25.0
7 6.0 26.0
8 7.0 27.0
9 0.0 27.1
10 0.0 27.2
11 0.0 27.3
12 10.0 30.0
13 11.0 31.0
14 12.0 32.0
The goal is, use the value of "my_key" to slice df and generate df1, df2 df3 like the expected output below. Note: my_var in this toy code has three entries, in my case, it may contain lengths different of three also, any of df1, df2 and df3 may containing different values as well.
df1:
A B
1.0 21.0
2.0 22.0
3.0 23.0
df2:
A B
5.0 25.0
6.0 26.0
7.0 27.0
and df3:
A B
10.0 30.0
11.0 31.0
12.0 32.0
Upvotes: 0
Views: 141
Reputation: 61920
You could do:
eq_key = df['A'].eq(my_key)
groups = (eq_key != eq_key.shift(1)).cumsum()
res = [group for _, group in df[~eq_key].groupby(groups[~eq_key])]
for g in res:
print(g)
Output
A B
0 1.0 21.0
1 2.0 22.0
2 3.0 23.0
A B
6 5.0 25.0
7 6.0 26.0
8 7.0 27.0
A B
12 10.0 30.0
13 11.0 31.0
14 12.0 32.0
Upvotes: 1