DaveM
DaveM

Reputation: 147

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

Upvotes: 0

Views: 47

Answers (2)

MrNobody33
MrNobody33

Reputation: 6483

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

Upvotes: 1

bigbounty
bigbounty

Reputation: 17408

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

Upvotes: 1

Related Questions