Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert String(pipe delimited) to a df in Pandas

I have the below string(pipe delimited) and I’m trying to convert it into a df in pandas but failing, could you guys help me

list = 'PP_AACD_NR_D8706_TIHIBRIDA_PROC_EXCUC_D|PP_AACE_R4539_BACEN|PP_AACE_R4539_CARGA_INT|PP_AACE_R4539_CONS_JUNC|PP_AACE_R4539_FMRC_TD_01'

I tried some things, and none has worked:

df1 = pd.DataFrame(list)

also:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from csv import reader
df=pd.DataFrame( list(reader(list)))

and other things, what I’m trying to achieve is a df like this:

column_name
PP_AACD_NR_D8706_TIHIBRIDA_PROC_EXCUC_D
PP_AACE_R4539_BACEN
PP_AACE_R4539_CARGA_INT
PP_AACE_R4539_CONS_JUNC
PP_AACE_R4539_FMRC_TD_01

>Solution :

You need to splityour str by the ‘|’ delimiter like :

import pandas as pd

l = 'PP_AACD_NR_D8706_TIHIBRIDA_PROC_EXCUC_D|PP_AACE_R4539_BACEN| \
    PP_AACE_R4539_CARGA_INT|PP_AACE_R4539_CONS_JUNC|PP_AACE_R4539_FMRC_TD_01'

df = pd.DataFrame(l.split('|'), columns=['col_1'])

print(df)

output:

                                     col_1
0  PP_AACD_NR_D8706_TIHIBRIDA_PROC_EXCUC_D
1                      PP_AACE_R4539_BACEN
2                  PP_AACE_R4539_CARGA_INT
3                  PP_AACE_R4539_CONS_JUNC
4                 PP_AACE_R4539_FMRC_TD_01

Also avoid using generic names as the names of your variables,it might resort to some issues (like : list, dict, …)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading