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

一笑奈何 提交于 2020-03-21 10:57:07

问题


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.


回答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



回答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.



来源:https://stackoverflow.com/questions/33700626/how-to-convert-tuple-of-tuples-to-pandas-dataframe-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!