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
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
You can try this:
lst = lst.split('|')
df = pd.DataFrame({'column_name': lst})
print(df)
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, ...)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.