1

I'm trying to "convert" the information of the dataframe rows 0 to 15 and columns col1 to col 16 into an image (16x16)

I'm reading the dataframe from a .txt file:

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

After creating an empty matrix img = np.zeros((16, 16, 3), dtype=np.uint8) , I want to iterate over the dataframe to transfer the columns information.

For that, I thought using df.itertuples but I'm having problems in filling the brackets.

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

Can you provide any advice? Thanks.

az_
  • 11
  • 3

1 Answers1

0

Use DataFrame.iloc + np.resize

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

if your columns are not aligned to the right you can filter with 'col'

np.resize(df.filter(regex='col').iloc[:16, :].to_numpy(), (16, 16, 3))
ansev
  • 28,746
  • 5
  • 11
  • 29
  • Thank you for your answer! If I may, what if the dataframe has more than 16 lines? For example, if it has 48 lines, how can I iterate and sum the pixel values of the 3 images(16x16)? – az_ Nov 09 '20 at 18:27
  • I think this is a separate question, I don't know what you mean – ansev Nov 09 '20 at 18:30
  • 1
    Ok, i will post a new question. Thanks! – az_ Nov 09 '20 at 18:34
  • Hi @ansev , i posted the question here: https://stackoverflow.com/questions/64760437/iterate-and-transfer-dataframe-information-to-an-image – az_ Nov 09 '20 at 22:56