Python 如何将 numpy 数组转换为 Pandas 数据框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21938932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:54:44  来源:igfitidea点击:

How do I convert a numpy array into a pandas dataframe?

pythonnumpypandas

提问by Hugo

I would like to have the 3 columns of a numpy array

我想要一个 numpy 数组的 3 列

px[:,:,0]
px[:,:,1]
px[:,:,0]

into a pandas Dataframe.

进入熊猫数据框。

Should I use?

我应该使用吗?

df = pd.DataFrame(px, columns=['R', 'G', 'B'])

Thank you

谢谢

Hugo

雨果

采纳答案by Alvaro Fuentes

You need to reshape your array first, try this:

你需要先重塑你的数组,试试这个:

px2 = px.reshape((-1,3))
df = pd.DataFrame({'R':px2[:,0],'G':px2[:,1],'B':px2[:,2]})