Convert String(pipe delimited) to a df in Pandas

Question:

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:

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
Asked By: gfernandes

||

Answers:

You need split the string by | into list

df = pd.DataFrame({'column_name': list.split('|')})
print(df)

                               column_name
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
Answered By: Ynjxsjmh

You can try this:

lst = lst.split('|')
df = pd.DataFrame({'column_name': lst})
print(df)
Answered By: Python29

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, …)

Answered By: mrCopiCat

First of all, don’t use a builtin name list.
You can split your pipe-delimited string using .split()

For example:

split_list = a.split("|")

This will give you a list with all the desired column names. From there you can create your Dataframe.

Answered By: OlafdeL
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.