Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide a dataframe based on categorical variables?

I have a dataset where for some people credit card application is accepted while for others it is declined.

I want to divide the dataset into two datasets; one for which all the credit cards are accepted(card='yes') and the other for which all the credit cards are declined(card='no').

The dataset is as shown below:

enter image description here

How can I do that?

like image 232
Soumee Avatar asked Nov 27 '25 18:11

Soumee


1 Answers

One option is to perform a groupby operation inside a dict comprehension. This has the added benefit of working for an arbitrary number of categories.

dfs_by_card = {
   accepted: sub_df
   for accepted, sub_df in credit5.groupby("card")
}
like image 114
PMende Avatar answered Nov 29 '25 07:11

PMende



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!