如何将 Numpy 数组转换为 Panda DataFrame
- 作者: 醉着的马儿不由缰
- 来源: 51数据库
- 2022-12-23
问题描述
我有一个如下所示的 Numpy 数组:
I have a Numpy array that looks like this:
[400.31865662] [401.18514808] [404.84015554] [405.14682194] [405.67735105] [273.90969447] [274.0894528]
当我尝试使用以下代码将其转换为 Panda Dataframe
When I try to convert it to a Panda Dataframe with the following code
y = pd.DataFrame(data) print(y)
打印时我得到以下输出.为什么我会得到所有这些零?
I get the following output when printing it. Why do I get all those zéros?
0
0 400.318657
0
0 401.185148
0
0 404.840156
0
0 405.146822
0
0 405.677351
0
0 273.909694
0
0 274.089453
我想得到一个看起来像这样的单列数据框:
I would like to get a single column dataframe which looks like that:
400.31865662 401.18514808 404.84015554 405.14682194 405.67735105 273.90969447 274.0894528
推荐答案
你可以 展平 numpy 数组:
You could flatten the numpy array:
import numpy as np
import pandas as pd
data = [[400.31865662],
[401.18514808],
[404.84015554],
[405.14682194],
[405.67735105],
[273.90969447],
[274.0894528]]
arr = np.array(data)
df = pd.DataFrame(data=arr.flatten())
print(df)
输出
0 0 400.318657 1 401.185148 2 404.840156 3 405.146822 4 405.677351 5 273.909694 6 274.089453
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
