Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a dataframe based on column values, sort or groupby for string values?

Completely new to coding and pandas.

df

   Date         Particulars    Inwards  Code

1 2017-04-01         EFG           12800    01
2 2017-07-22         ABC           100      01
3 2017-09-05         BCD           10000    01
4 2018-03-13         ABC           2000     01

I wanted to output 3 dataframes from this df based on the df['Particulars'] column, i.e.

Output: df1

   Date         Particulars    Inwards  Code

2 2017-07-22         ABC           100      01
4 2018-03-13         ABC           2000     01

df2

   Date         Particulars    Inwards  Code

1 2017-04-01         EFG           12800    01

df3

   Date         Particulars    Inwards  Code

3 2017-09-05         BCD           10000    01

I have a way of doing it through:

 df1 = df1.append(df.loc[df['Particulars'] == 'ABC'], ignore_index=False)

while I initialise a list of Particulars and make dataframes and then do the above command using a for loop. But I am wondering if sort or groupby would be better options? And how exactly to apply them I tried groupby and sort but can't get the dataframe.

like image 848
Sid Avatar asked May 04 '26 03:05

Sid


1 Answers

You can create a dictionary of data frames by grouping your df on Particulars.

d = {index: label for index, label in df.groupby('Particulars')}

Now you can access each df using

d['ABC']

    Date        Particulars Inwards Code
2   2017-07-22  ABC         100     1
4   2018-03-13  ABC         2000    1
like image 121
Vaishali Avatar answered May 05 '26 15:05

Vaishali