34

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.

Oleg Melnikov
  • 3,080
  • 3
  • 34
  • 65

2 Answers2

54
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
furas
  • 134,197
  • 12
  • 106
  • 148
10

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677