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.

Todd Muller
  • 11
  • 2
  • 7

1 Answers1

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

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • 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? – Todd Muller Jun 15 '18 at 16:17
  • How do we access the dataframes created ? Also if we want to group by 2 columns how will we access the dataframe ? – user6016731 Dec 17 '19 at 14:17
  • df which should be dfs and is an array so access by index. – Anton vBR Dec 17 '19 at 17:21