1

There's probably a simple solution to this that I just couldn't find... With the given DataFrame, how can I separate it into multiple DataFrames and go from something like:

>>>import pandas as pd
>>>d ={'LOT': [102,104,162,102,104,102],'VAL': [22,424,65,4,34,6]}
>>>df = pd.DataFrame(data=d)
>>>df

   LOT  VAL
0  102   22
1  104  424
2  162   65
3  102    4
4  104   34
5  102    6

to:

>>>df[0]
   LOT  VAL
0  102   22
1  102    4
2  102    6
>>>df[1]
   LOT  VAL
0  104  424
1  104   34
>>>df[2]
   LOT  VAL
0  162   65

With 3 distinct DataFrames Please let me know if you need more information.

0

1 Answer 1

4

This is a simple groupby. Let me see if I find a dupe:

import pandas as pd

df = pd.DataFrame({
    'LOT': [102,104,162,102,104,102],
    'VAL': [22,424,65,4,34,6]
})

df = [x for _, x in df.groupby('LOT')]

Ok, I found something. However the answer seems overcomplicated so I'm gonna leave this here. Looks a lot like: Split pandas dataframe based on groupby

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

3 Comments

That helps a lot! I'm not very familiar with that type of for loop, do you have any suggestions where I could go to learn more about it? [In particular, how it works having the x before "for" as well as after the comma?
How do we access the dataframes created ? Also if we want to group by 2 columns how will we access the dataframe ?
df which should be dfs and is an array so access by index.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.