【问题标题】:Transfer some dataframe information to a new matrix将一些数据帧信息传输到新矩阵
【发布时间】:2020-11-09 18:11:17
【问题描述】:

我正在尝试将数据帧行 0 到 15 和列 col1 到 col 16 的信息“转换”为图像 (16x16)

我正在从 .txt 文件中读取数据框:

df = pd.read_csv('User1/Video1.txt', sep="\s+|\t+|\s+\t+|\t+\s+", header=None, names=headers, engine='python', parse_dates=parse_dates)
                        date arrow  col1  col2  ...  col13  col14  col15  col16
0    2020-11-09 09:53:39.552    ->   0.0   0.0  ...    0.0    0.0    0.0    0.0
1    2020-11-09 09:53:39.552    ->   0.0   2.0  ...    0.0    0.0    0.0    0.0
2    2020-11-09 09:53:39.552    ->   0.0   0.0  ...    0.0    0.0    6.0    6.0
3    2020-11-09 09:53:39.552    ->   0.0   0.0  ...    0.0    0.0    0.0    0.0
4    2020-11-09 09:53:39.586    ->   0.0   9.0  ...    0.0    7.0    0.0    0.0
...                      ...   ...   ...   ...  ...    ...    ...    ...    ...
15   2020-11-09 09:54:06.920    ->   4.0   0.0  ...    4.0    4.0    0.0    0.0

创建空矩阵img = np.zeros((16, 16, 3), dtype=np.uint8) 后,我想遍历数据框以传输列信息。

为此,我想使用df.itertuples,但我在填写括号时遇到了问题。

for row in df.itertuples():
     img[]][] += row[]

你能提供什么建议吗?谢谢。

【问题讨论】:

    标签: python arrays pandas dataframe


    【解决方案1】:

    使用DataFrame.iloc + np.resize

    #import numpy as np
    np.resize(df.iloc[:16, -16:].to_numpy(), (16, 16, 3))
    

    如果您的列未向右对齐,您可以使用 'col' 过滤

    np.resize(df.filter(regex='col').iloc[:16, :].to_numpy(), (16, 16, 3))
    

    【讨论】:

    • 感谢您的回答!如果可以,如果数据框的行数超过 16 行怎么办?例如,如果它有 48 行,我该如何迭代和求和 3 张图像(16x16)的像素值?
    • 我认为这是一个单独的问题,我不知道你的意思
    • 好的,我会发布一个新问题。谢谢!
    • 嗨@ansev,我在这里发布了问题:stackoverflow.com/questions/64760437/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多