Python Forum
[pandas] How to reshape the list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Data Science (https://python-forum.io/Forum-Data-Science)
+--- Thread: [pandas] How to reshape the list (/Thread-pandas-How-to-reshape-the-list)



[pandas] How to reshape the list - Mekala - Jul-25-2020

Hi,
I have blow list, I can reshape it but I want to reshape based on other condition:

#%%
import pandas as pd
import numpy as np
lst = ['A','',3,'A','',0,'A',3,2]
aa = np.reshape(lst,(-1,3))
aa = pd.DataFrame(aa, columns=['A','B','C'])
but I want to break the sub-list at each 'A'
A	B	C
A		3
A		0
A	3	2
I want to break at 'A' because some time columns may change,
if I have a list like:

lst = ['A','',3,'4','A','',0,'2020/07/25 10:13:23','A',3,2,''2020/07/25 10:13:29']
in this case, i need to change np.reshape(lst,(-1,4)), which I want to avoide


RE: How to reshape the list - scidam - Jul-25-2020

You can use np.split and np.where to do this
np.vstack(np.split(lst, np.where(np.array(lst) == 'A')[0][1:]))[:,1:]
Output:
array([['', '3', '4'], ['', '0', '2020/07/25 10:13:23'], ['3', '2', '2020/07/25 10:13:29']], dtype='<U19')



RE: How to reshape the list - Mekala - Jul-25-2020

Though it splits at "A", but I want to keep "A" as my first column row element.
'A','',3,'4'
'A','',0,'2020/07/25 10:13:23'
'A',3,2,''2020/07/25 10:13:29'



RE: How to reshape the list - scidam - Jul-25-2020

(Jul-25-2020, 05:25 AM)Mekala Wrote: Though it splits at "A", but I want to keep "A" as my first column row element.
Just remove [:,1:] from the previous solution.


RE: [pandas] How to reshape the list - Mekala - Jul-25-2020

what is function of [0][1:])? may I learn.


RE: [pandas] How to reshape the list - scidam - Jul-26-2020

np.where returns a tuple. The first item of this tuple is an array of indecies where A is occurred. So, we need to pass np.where(...)[0] to the np.split function. However, the first element of lst is 'A' and we don't need to split array at this position. So, we just excluded the index of first occurrence of A from splittng [1:] -- means select all indecies but not the first (first is 0).
Thus, [0] -- selects the first element of the tuple that is an array of indecies where A is occurred. and [1:] selects all such indecies except the first.


RE: [pandas] How to reshape the list - Mekala - Jul-26-2020

OK, Thanks.