35

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.

0

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

11

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.

1 Comment

I had this: pd.DataFrame(json.loads(json.dumps(s))) but yours is cleaner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.