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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With