muazfaiz
muazfaiz

Reputation: 5021

Convert Tuple of Date and list into Pandas Dataframe

I have a list of tuple and I want to convert it into a dataframe. I am doing like something this:

a = (date1, [1,2,3,4,5,6]) 
frame = pandas.DataFrame(a)

Also I write this to a csv file:

frame.to_csv('frame.csv', sep = ',', encoding='utf-8')

But the problem is that it writes in the csv file like the following:

0         | 1
date1     | [1,2,3,4,5,6]

I want it to write like this.

0         | 1   |2    |3    |4    |5    |6
date1     | 1   |2    |3    |4    |5    |6

How can I do this ? Thanks.

Upvotes: 2

Views: 344

Answers (1)

jezrael
jezrael

Reputation: 862771

I think you can create DataFrame from list by convert first item of tuple to one item list [a[0]] and add it to nested list a[1]:

date1 = pd.to_datetime('2015-01-01')
a = (date1, [1,2,3,4,5,6]) 

L = [a[0]] + a[1]
print (L)
[Timestamp('2015-01-01 00:00:00'), 1, 2, 3, 4, 5, 6]

frame = pd.DataFrame([L])
print (frame)
           0  1  2  3  4  5  6
0 2015-01-01  1  2  3  4  5  6

Upvotes: 2

Related Questions