Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split pandas dataframe into list of dataframes by id?

I have a big pandas dataframe (about 150000 rows). I have tried method groupby('id') but in returns group tuples. I need just a list of dataframes, and then I convert them into np array batches to put into an autoencoder (like this https://www.datacamp.com/community/tutorials/autoencoder-keras-tutorial but 1D)

So I have a pandas dataset :

data = {'Name': ['Tom', 'Joseph', 'Krish', 'John', 'John', 'John', 'John', 'Krish'], 'Age': [20, 21, 19, 18, 18, 18, 18, 18],'id': [1, 1, 2, 2, 3, 3, 3, 3]}  
# Create DataFrame  
df = pd.DataFrame(data)  
# Print the output.  
df.head(10)

I need the same output (just a list of pandas dataframe). Also, i need a list of unsorted lists, it is important, because its time series.

data1 = {'Name': ['Tom', 'Joseph'], 'Age': [20, 21],'id': [1, 1]}  
data2 = {'Name': ['Krish', 'John', ], 'Age': [19, 18, ],'id': [2, 2]}  
data3 = {'Name': ['John', 'John', 'John', 'Krish'], 'Age': [18, 18, 18, 18],'id': [3, 3, 3, 3]}  
pd_1 = pd.DataFrame(data1)
pd_2 = pd.DataFrame(data2)
pd_3 = pd.DataFrame(data3)
array_list = [pd_1,pd_2,pd_3]
array_list

How can I split dataframe ?

like image 360
Семен Немытов Avatar asked Nov 17 '25 02:11

Семен Немытов


2 Answers

Or you can TRY:

array_list = df.groupby(df.id.values).agg(list).to_dict('records')

Output:

[{'Name': ['Tom', 'Joseph'], 'Age': [20, 21], 'id': [1, 1]},
 {'Name': ['Krish', 'John'], 'Age': [19, 18], 'id': [2, 2]},
 {'Name': ['John', 'John', 'John', 'Krish'],
  'Age': [18, 18, 18, 18],
  'id': [3, 3, 3, 3]}]

UPDATE:

If you need a dataframe list:

df_list = [g for _,g in df.groupby('id')]
#OR
df_list = [pd.DataFrame(i) for i in df.groupby(df.id.values).agg(list).to_dict('records')]

To reset the index of each dataframe:

df_list = [g.reset_index(drop=True) for _,g in df.groupby('id')]
like image 135
Nk03 Avatar answered Nov 19 '25 16:11

Nk03


Let us group on id and using to_dict with orientation list prepare records per id

[g.to_dict('list') for _, g in df.groupby('id', sort=False)]

[{'Name': ['Tom', 'Joseph'], 'Age': [20, 21], 'id': [1, 1]},
 {'Name': ['Krish', 'John'], 'Age': [19, 18], 'id': [2, 2]},
 {'Name': ['John', 'John', 'John', 'Krish'], 'Age': [18, 18, 18, 18], 'id': [3, 3, 3, 3]}]
like image 23
Shubham Sharma Avatar answered Nov 19 '25 15:11

Shubham Sharma