Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split based on factors alternative of R function in python

   df = 
    v1  v2
    ds  43
    ds  34
    ds  32
    foo 34
    foo 32

In R we can create list of dataframes using

h = split(df,as.factor(df$v1))
output
h:
[[1]]
v1   v2
ds   43
ds   34
ds   32
[[2]]
v1   v2
foo  34
foo  32

What is the alternative for creating list of dataframes based upon different values in single column in python

i tried groupby in python but the answer which i am getting is different

df = df.groupby('v1').groups
like image 415
koneru nikhil Avatar asked Sep 02 '25 17:09

koneru nikhil


1 Answers

You can use:

h = [g for _, g in df.groupby('v1')]

to get a list of data frames.

like image 135
Psidom Avatar answered Sep 05 '25 07:09

Psidom