How To Convert a 3D Array To a Dataframe(如何将3D数组转换为数据帧)
本文介绍了如何将3D数组转换为数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个维度数组(40 X 40 X 8064),它对应于(视频X频道X数据)。
但现在我想按如下顺序将数组转换为数据框:
Index | Video | Channel_0 | Channel_1 | Channel_2 | .... | Channel_39
0 | 0 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
1 | 0 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
2 | 0 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
3 | 0 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
4 | 0 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
...............
8063 | 0 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
......
......
322559 | 39 |[Some Value] |[Some Value] |[Some Value] | .... |[Some Value]
推荐答案
import numpy as np
import pandas as pd
n_videos = 2
n_channels = 3
n_points = 4
# Generate data
A = np.arange(n_videos * n_channels * n_points).reshape(n_videos,
n_channels,
n_points)
col_names = ["Channel_{}".format(i) for i in range(n_channels)]
# Need to re-order axis before reshape.
# Want axis correspoonding to videos to be at first axis.
df = pd.DataFrame(np.rollaxis(A, 2, 1).reshape(-1, n_channels),
columns=col_names)
video_idx = np.hstack((np.full((n_points,), i) for i in range(n_videos)))
video_idx = pd.Series(video_idx, name="Video")
df.insert(0, "Video", video_idx)
这篇关于如何将3D数组转换为数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:如何将3D数组转换为数据帧
基础教程推荐
猜你喜欢
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
