Search code examples
pythondataframedata-analysis

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

Solution

  • You can use:

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

    to get a list of data frames.