I have an array that has values separated by '|'. I would like to parse it to a pandas data frame.
import pandas as pd
arr = ['19345360853|5264654|100530|2017-01-07', '19345360853|13518371|100530|2018-10-08']
pd.DataFrame([{'Id': item.split('|')[0] ,'Code_A': item.split('|')[1] , 'Code_B': item.split('|')[2],'Reg_Date': item.split('|')[3]} for item in arr ])
I would like the pandas dataframe to be in the following schema,
'Id' string 'Code_A' string 'Code_B' string 'Reg_Date' date
So the resulting Pandas dataframe would be similar to this. result dataframe
Any help is appreciated.
First, convert to two dimensional list
arr = [a.split("|") for a in arr]
Second, convert to pandas dataframe
data = pd.DataFrame(arr,columns=['Id','Code_A','Code_B','Reg_Date'])
Id Code_A Code_B Reg_Date
0 19345360853 5264654 100530 2017-01-07
1 19345360853 13518371 100530 2018-10-08
Convert column Reg_Date using astype
(Ref: astype )
a =pd.DataFrame(arr,columns=['Id','Code_A','Code_B','Reg_Date'])
a['Reg_Date'] = a['Reg_Date'].astype('datetime64[ns]')
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.