0

I have a below dataframe

        df = 
        city         value
        hyderabad     1
        chennai       1
        mumbai        2
        pune          2
        delhi         3
        nodia         3
    

I want to split the dataframe into multiple dataframes to generate one combination for each dataframe, excluding the rows which has same value in a loop. The result sets would look like below.

df =  city         value
      hyderabad     1
      mumbai        2
      delhi         3
      
df =  city         value
      hyderabad     1
      mumbai        2
      nodia         3
      
      
df =  city         value
      hyderabad     1
      pune          2
      delhi         3
      
df =  city         value
      hyderabad     1
      pune          2
      nodia         3

      
df =  city         value
      chennai        1
      mumbai         2
      delhi          3
      
df =  city         value
      chennai        1
      mumbai         2
      nodia          3

df =  city         value
      chennai        1
      pune           2
      nodia          3

df =  city         value
      chennai        1
      pune           2
      delhi          3

What is the simplest way to get this done, as i dont know how many rows of data I will have in advance.

1
  • What do you mean by 'into multiple dataframes'. You want a variable df1, df2, ..., dfN or a dict with a key for each combination? Commented Jul 6, 2021 at 21:38

1 Answer 1

1

Try:

from itertools import product

for c in product(*[g.index.tolist() for _, g in df.groupby("value")]):
    print(df.loc[c, :])
    print()

Prints:

        city  value
0  hyderabad      1
2     mumbai      2
4      delhi      3

        city  value
0  hyderabad      1
2     mumbai      2
5      nodia      3

        city  value
0  hyderabad      1
3       pune      2
4      delhi      3

        city  value
0  hyderabad      1
3       pune      2
5      nodia      3

      city  value
1  chennai      1
2   mumbai      2
4    delhi      3

      city  value
1  chennai      1
2   mumbai      2
5    nodia      3

      city  value
1  chennai      1
3     pune      2
4    delhi      3

      city  value
1  chennai      1
3     pune      2
5    nodia      3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.