Chenxiao XU
Chenxiao XU

Reputation: 31

create a Pandas DataFrame from a series of list

I have a Pandas series of list

0 ['2018-3-13', '16:00']
1 ['2018-3-13', '16:01']
2 ['2018-3-13', '16:02']
3 ['2018-3-13', '16:03']
.
.
.

And I want to create DataFrame as

0 '2018-3-13' '16:00'
1 '2018-3-13' '16:01'
2 '2018-3-13' '16:02'
3 '2018-3-13' '16:03'
...

So anyone could provide a innovative idea?

Quite thanks

Upvotes: 2

Views: 72

Answers (2)

Ben.T
Ben.T

Reputation: 29635

you can use apply and pd.Series such as:

# with jpp data
s = pd.Series([['2018-3-13', '16:00'], ['2018-3-13', '16:01'],
               ['2018-3-13', '16:02'], ['2018-3-13', '16:03']])
# then you use apply and rename
df = s.apply(pd.Series).rename(columns={0:'date',1:'time'})

just another method

Upvotes: 0

jpp
jpp

Reputation: 164613

One way is to extract the NumPy array representation and use np.ndarray.tolist. You can then feed into pd.DataFrame:

import pandas as pd

s = pd.Series([['2018-3-13', '16:00'], ['2018-3-13', '16:01'],
               ['2018-3-13', '16:02'], ['2018-3-13', '16:03']])

df = pd.DataFrame(s.values.tolist(), columns=['date', 'time'])

print(df)

        date   time
0  2018-3-13  16:00
1  2018-3-13  16:01
2  2018-3-13  16:02
3  2018-3-13  16:03

Upvotes: 2

Related Questions