将元组转换为数据帧(Converting tuple into dataframe)

编程入门 行业动态 更新时间:2024-10-26 20:30:16
将元组转换为数据帧(Converting tuple into dataframe)

我有这样的元组:

x=(('a', 'b'), ('foo', 'bar'))

我想把它变成像这样的DataFrame:

One Two Three Four a b foo bar

我一直试图用这个:

df = pd.DataFrame(x, columns=['One', 'Two', 'Three', 'Four])

但是会返回此错误:

runfile('D:/python codes/histo_matching.py', wdir='D:/python codes') Traceback (most recent call last): File "<ipython-input-31-1104531b1d67>", line 1, in <module> runfile('D:/python codes/histo_matching.py', wdir='D:/python codes') File "C:\Users\Stefano\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Users\Stefano\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "D:/python codes/histo_matching.py", line 63, in <module> df = pd.DataFrame(x, columns=['One', 'Two', 'Three', 'Four']) File "C:\Users\Stefano\Anaconda\lib\site-packages\pandas\core\frame.py", line 291, in __init__ raise PandasError('DataFrame constructor not properly called!') PandasError: DataFrame constructor not properly called!

I have a tuple like this:

x=(('a', 'b'), ('foo', 'bar'))

and I want to turn it into a DataFrame like this:

One Two Three Four a b foo bar

I have been trying to use this:

df = pd.DataFrame(x, columns=['One', 'Two', 'Three', 'Four])

but this error is returned:

runfile('D:/python codes/histo_matching.py', wdir='D:/python codes') Traceback (most recent call last): File "<ipython-input-31-1104531b1d67>", line 1, in <module> runfile('D:/python codes/histo_matching.py', wdir='D:/python codes') File "C:\Users\Stefano\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Users\Stefano\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "D:/python codes/histo_matching.py", line 63, in <module> df = pd.DataFrame(x, columns=['One', 'Two', 'Three', 'Four']) File "C:\Users\Stefano\Anaconda\lib\site-packages\pandas\core\frame.py", line 291, in __init__ raise PandasError('DataFrame constructor not properly called!') PandasError: DataFrame constructor not properly called!

最满意答案

您可以使用list(sum(x, ()))来展平tuple的tuples :

x = (('a', 'b'), ('foo', 'bar')) pd.DataFrame(data=list(sum(x, ())), index=['One', 'Two', 'Three', 'Four']).transpose() One Two Three Four 0 a b foo bar

You can use list(sum(x, ())) to flatten your tuple of tuples:

x = (('a', 'b'), ('foo', 'bar')) pd.DataFrame(data=list(sum(x, ())), index=['One', 'Two', 'Three', 'Four']).transpose() One Two Three Four 0 a b foo bar

更多推荐

本文发布于:2023-08-05 19:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   数据   将元组   dataframe   tuple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!