0

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

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

1 Comment

How to get result in wide format such as instead of three columns I have nine such as: day_1 date_1 fruuit_1 day_2 date_2 fruit_2 day_3 date_3 fruit_3
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.