Search code examples
pythonstringpandasdataframefpgrowth

Convert string formatted as Pandas DataFrame into an actual DataFrame


I am trying to convert a formatted string into a pandas data frame.

[['CD_012','JM_022','PT_011','CD_012','JM_022','ST_049','MB_021','MB_021','CB_003'
,'FG_031','PC_004'],['NL_003','AM_006','MB_021'],
['JA_012','MB_021','MB_021','MB_021'],['JU_006'],
['FG_002','FG_002','CK_055','ST_049','NM_004','CD_012','OP_002','FG_002','FG_031',
'TG_005','SP_014'],['FG_002','FG_031'],['MD_010'],
['JA_012','MB_021','NL_003','MZ_020','MB_021'],['MB_021'],['PC_004'],
['MB_021','MB_021'],['AM_006','NM_004','TB_006','MB_021']]

I am trying to use the pandas.DataFrame method to do so but the result is that this whole string is placed inside one element in the DataFrame.


Solution

  • Best approach would be to split the string with the '],[' delimeter and then convert to df.

    
    import numpy as np
    import pandas as pd
    
    def stringToDF(s):
        array = s.split('],[')
    
        # Adjust the constructor parameters based on your string
        df = pd.DataFrame(data=array,    
                  #index=array[1:,0],    
                 #columns=array[0,1:]
                 ) 
    
        print(df)
        return df
    
    stringToDF(s)
    
    

    Good luck!