Split dataframe based on number of rows with a column value
Does this work? df.groupby('animal', group_keys=False).apply(lambda x: x.sample(frac=0.2))  You could then remove these rows from your original dataframe to create the one with 40 of each animal.
I have a dataframe that has an animals column with different animals (say ["cat", "dog", "lion"]) as rows and a value corresponding to each animal. There are 10 unique animals and 50 entries of each. The animals are not in any particular order.
I want to split the dataframe into two with one containing 40 of each animal and another containing 10 of each animal. That is one dataframe should contain 40 cats, 40 dogs etc and the other dataframe with 10 cats, 10 dogs etc.
Any help would be greatly appreciated.
I have tried to sort by unique values but it did not work. I am not very familiar with Pandas yet and this is the first time I am using it.
Edit:
Adding a better example of what I need




Animal
value




dog
12


cat
14


dog
10


cat
40


dog
90


dog
80


cat
30


dog
20


cat
20


cat
23




I want to separate this into 2 data frames. In this example the first dataframe would have 3 of each animal and the other one would have 2 of each animal.




Animal
value




dog
12


dog
10


dog
90


cat
14


cat
40


cat
30







Animal
value




dog
80


dog
20


cat
20


cat
23