2

I would like to split a big dataframe into several smaller dataframes according to the value from the first column if that's possible, I didn't find it online. Example, I have this:

DF
Column1 Column2 Column3 Column4 
  A       1       2       1
  A       1       1       2
  A       3       2       2
  B       2       1       2
  B       3       1       1

split this into :
DF1
Column1 Column2 Column3 Column4 
  A       1       2       1
  A       1       1       2
  A       3       2       2

DF2
Column1 Column2 Column3 Column4 
  B       2       1       2
  B       3       1       1
DarkWarrior
  • 114
  • 1
  • 11
  • Does this answer your question? [Splitting dataframe into multiple dataframes](https://stackoverflow.com/questions/19790790/splitting-dataframe-into-multiple-dataframes) – Shaido Sep 11 '20 at 09:28

3 Answers3

0

Simple as:

df1 = df[df['A'] == 'A']
df2 = df[df['B'] == 'B']

if you have unique values for the column you could create a list of dataframes:

df_lst = list()
unique_elements = df['column1'].unique()

for elm in unique_elements:
     df_lst.append(df[df['column1'] == elm])
Piero Costa
  • 180
  • 11
0

you can solve this by pandas and use the function groupby()

import pandas as pd
#load your file 
df =pd.read_csv('sample.csv')
grouped = df.groupby(df.column1)
A = grouped.get_group("A")
B = grouped.get_group("B")
print(A)
print(B)
Alan Jose Tom
  • 131
  • 2
  • 5
0

You can use groupby as:

df=[['A',1 ,1,2],['A',10,1,2],['B',10,1,2],['B',30,1,2]]
df = pd.DataFrame(df,columns=['a','b','c','d'])

d1,d2 = df.groupby('a')
print(d1[1])
print()
print(d2[1])

   a   b  c  d
0  A   1  1  2
1  A  10  1  2

   a   b  c  d
2  B  10  1  2
3  B  30  1  2
Sam S.
  • 627
  • 1
  • 7
  • 23