How to convert tuple of tuples to pandas.DataFrame in Python?
import pandas as pds = ((1,0,0,0,),(2,3,0,0,),(4,5,6,0,),(7,8,9,10,))print pd.DataFrame(list(s))# 0 1 2 3# 0 1 0 0 0# 1 2 3 0 0# 2 4 5 6 0# 3 7 8 9 10print pd.DataFrame(list(s), columns=[1,2,3,4], index=[1,2,3,4]) # 1 2 3 4# 1 1 0 0 0# 2 2 3 0 0# 3 4 5 6 0# 4 7 8 9 10
Pass a list of tuples instead of a tuple of tuples:
In [13]: pd.DataFrame(list(s))Out[13]: 0 1 2 30 1 0 0 01 2 3 0 02 4 5 6 03 7 8 9 10
pd.DataFrame(data)
follows different code paths when data
is a tuple as opposed to a list.
Pandas developer Jeff Reback explains:
list-of-tuples is the specified type, tuple-of-tuple is not allowed as I think it can signify nested types that would require more parsing.