Python Forum
[pandas] How to reshape the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pandas] How to reshape the list
#1
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
Reply
#2
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')
Reply
#3
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'
Reply
#4
(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.
Reply
#5
what is function of [0][1:])? may I learn.
Reply
#6
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.
Reply
#7
OK, Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cannot reshape array of size 0 into shape Roro 2 1,270 Jun-14-2020, 11:28 AM
Last Post: Roro
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 535 Jan-28-2020, 09:30 AM
Last Post: axelle
  reshape from 3D to 3D matrix paul18fr 0 411 Nov-12-2019, 11:26 AM
Last Post: paul18fr
  Simple numpy reshape error wih contour3D AdeIsHere 0 544 Sep-17-2019, 12:01 PM
Last Post: AdeIsHere
  Inserting data from python list into a pandas dataframe mahmoud899 0 833 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 8,157 Nov-04-2018, 10:38 PM
Last Post: stullis
  List and Dictionaries with Pandas Balinor 3 1,032 Aug-20-2018, 10:47 PM
Last Post: ichabod801
  Pandas - Write to Exisitng Excel File - Sorted List dj99 4 5,494 Jul-29-2018, 07:56 AM
Last Post: dj99
  missing pandas even if in conda list metalray 2 1,493 May-29-2018, 12:52 PM
Last Post: metalray
  1 I cant read many stocks(list) with pandas-datareader from yahoo Davidii111 1 2,299 Dec-27-2017, 04:47 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020