I have a DataFrame A with one column location_ms. I want to split by ; and : to get DataFrame B.
DataFrame A(Beginning):
DataFrame B(Final):
My code below seems to be very roundabout and would love to see a better implementation towards the problem. By doing the splits I create a DataFrame with each element being a list of lists. Then I flatten that list of lists to create the final DataFrame.
def locpapersrc_table(df):
toflattenrows = df['location_ms'].str.split(';').apply(lambda x:[c.split(':') for c in x]).values.tolist()
singlelistoflist = [item for sublist in toflatten for item in sublist]
tmp = pd.DataFrame(singlelistoflist)
return tmp
This version2 is slower than the first but is another method that is also very roundabout.
def version2(df):
xx = df["location_ms"].str.split(';',expand = True).T
tmp = pd.melt(xx).dropna().drop(['variable'],axis=1)['value'].str.split(':',expand=True)
return tmp
Thank You!

