Pandas Split Dataframe by Unique Column Value
In Pandas, you can split a dataframe based on unique values in a specific column using the groupby()
function. The groupby()
function groups the dataframe by the unique values in the specified column, allowing you to perform operations on each group separately.
Example:
Let’s say we have a dataframe named df
with the following data:
Col1 | Col2 |
---|---|
A | 1 |
B | 2 |
C | 3 |
A | 4 |
B | 5 |
Now, if we want to split the dataframe into separate dataframes based on the unique values in Col1
, we can use the following code:
grouped = df.groupby('Col1')
dfs = []
for key, group in grouped:
dfs.append(group)
# dfs will now contain separate dataframes for each group
In this example, the groupby()
function groups the dataframe df
by the values in Col1
. The loop then iterates over each group, and each group is appended to the list dfs
.
After running this code, dfs
will contain the following dataframes:
Dataframe 1:
Col1 | Col2 |
---|---|
A | 1 |
A | 4 |
Dataframe 2:
Col1 | Col2 |
---|---|
B | 2 |
B | 5 |
Dataframe 3:
Col1 | Col2 |
---|---|
C | 3 |
Each dataframe in dfs
represents a group of rows with the same value in Col1
. You can now perform operations on each individual dataframe if needed.