How to convert tuple of tuples to pandas.DataFrame in Python?

76,948

Solution 1

import pandas as pd

s = ((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  10

print 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

Solution 2

Pass a list of tuples instead of a tuple of tuples:

In [13]: pd.DataFrame(list(s))
Out[13]: 
   0  1  2   3
0  1  0  0   0
1  2  3  0   0
2  4  5  6   0
3  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.

Share:
76,948
Oleg Melnikov
Author by

Oleg Melnikov

http://oleg.rice.edu http://www.linkedin.com/in/olegmelnikov

Updated on February 06, 2020

Comments

  • Oleg Melnikov
    Oleg Melnikov about 4 years

    No offence, if the questions is too basic. Let me know if you need more information.

    I am looking for an idea to convert square-form tuple of tuples to pandas.DataFrame in a clean/efficient/pythonic way, i.e. from

    s =((1,0,0,0,),(2,3,0,0,),(4,5,6,0,),(7,8,9,10,))
    

    to pandas.DataFrame like

       1  2  3   4
    1  1  0  0   0
    2  2  3  0   0
    3  4  5  6   0
    4  7  8  9  10
    

    Naturally, this list can grow with more zeros appended in the upper-triangular (if we think of s as a tuple of rows).

    DataFrame(t) seems to fail.

  • skulz00
    skulz00 over 8 years
    I had this: pd.DataFrame(json.loads(json.dumps(s))) but yours is cleaner