'Split a single data frame into multiple data frames based on a columns value in pandas
I have a data frame that has thousands of rows in a Jupyter Notebook using Pandas. I am attempting to use a particular column in this data frame to split the df into multiple dfs based on the columns values. If there is a way to do this without specifying the different values in the column explicitly, that would be great.
| Col1 | Col2 | Col3 | Col4 |
|---|---|---|---|
| dat1 | Val1 | etc1 | set1 |
| dat2 | Val2 | etc2 | set2 |
| dat3 | Val3 | etc3 | set2 |
| dat4 | Val4 | etc4 | set3 |
An example of one of the variations of my code:
NAM_df2 = NAM_df1.loc[NAM_df1["Col4"] == 'set2']
Solution 1:[1]
Try this:
dfs = [d for _, d in df.groupby('Col4')]
Solution 2:[2]
what about
df1s = [df.loc[df['Col1']==x, :] for x in df['Col1'].unique()]
df4s = [df.loc[df['Col4']==x, :] for x in df['Col4'].unique()]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Roppon Picha |
