Split list and create a dataframe in python

I have a list of strings like this:

lis_val = ['Mon 01/12/2020 apple', 'Tue 01/13/2020 orange', 'Wed 01/14/2020 peach'] 

I need to assemble a dataframe from this list as:

df = Mon  01/12/2020 apple      Tue  01/13/2020 orange      Wed  01/14/2020 peach 
Add Comment
2 Answer(s)
In [82]: lis_val = ['Mon 01/12/2020 apple', 'Tue 01/13/2020 orange', 'Wed 01/14/2020 peach']     ...:  In [83]: pd.DataFrame([i.split() for i in lis_val]) Out[83]:      0           1       2 0  Mon  01/12/2020   apple 1  Tue  01/13/2020  orange 2  Wed  01/14/2020   peach 
Answered on July 16, 2020.
Add Comment

You could use pd.Series.str.split() with expand=True:

import pandas as pd  lis_val = ['Mon 01/12/2020 apple', 'Tue 01/13/2020 orange', 'Wed 01/14/2020 peach'] df=pd.DataFrame(lis_val)[0].str.split(expand=True) print(df) 

Output:

     0           1       2 0  Mon  01/12/2020   apple 1  Tue  01/13/2020  orange 2  Wed  01/14/2020   peach 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.