Suppose I have the following DataFrame:
ab 0 A 1.516733 1 A 0.035646 2 A -0.942834 3 B -0.157334 4 A 2.226809 5 A 0.768516 6 B -0.015162 7 A 0.710356 8 A 0.151429
And I need to group it according to "edge B"; this means that the groups will:
ab 0 A 1.516733 1 A 0.035646 2 A -0.942834 3 B -0.157334 4 A 2.226809 5 A 0.768516 6 B -0.015162 7 A 0.710356 8 A 0.151429
I.e. every time I find “B” in column “a”, I want to split my DataFrame.
My current solution:
#create the dataframe s = pd.Series(['A','A','A','B','A','A','B','A','A']) ss = pd.Series(np.random.randn(9)) dff = pd.DataFrame({"a":s,"b":ss})
and I got a dataframe:
ab grpb 0 A 1.516733 0 1 A 0.035646 0 2 A -0.942834 0 3 B -0.157334 0 4 A 2.226809 1 5 A 0.768516 1 6 B -0.015162 1 7 A 0.710356 2 8 A 0.151429 2
What can I break with dff.groupby('grpb') .
Is there a more efficient way to do this using pandas functions?
source share