是否可以将二维数组作为二维数据帧的元素?

发布于 2025-01-12 18:27:40 字数 270 浏览 5 评论 0原文

我是一名学生,正在使用视频编码器做我的项目。我已经提取了像素值和其他信息(POC、x 位置、y 位置、高度、宽度)并将它们保存在 txt 文件中。 我将使用 h5py 将这些数据转换为 hdf5 文件。但我想知道 pandas 甚至 hdf5 是否支持 pandas 2D 数据帧中的 2D 数组(像素值)。

例如,让 p = [[1,2],[3,4]] 作为我的像素值。我可以让我的数据框像 数据集[0] = [0(POC),0(x),0(y),p(2D 数组像素值)]? 或者甚至可以用 hdf5 格式编写?

I am a student doing my project with video encoder. I have extracted pixel values with other info (POC, x position, y position, height, width) with them in txt files.
I am going to convert these data into hdf5 files with h5py. But I wonder if pandas or even hdf5 support having a 2D-array(the pixel values) in a pandas 2D dataframe.

For example, let p = [[1,2],[3,4]] as my pixel value. May I have my dataframe be like
dataset[0] = [0(POC),0(x),0(y),p(2D array pixel values)]?
Or can it even written in hdf5 format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2025-01-19 18:27:41

是的,你可以,这里有一个例子:

df = pd.DataFrame({'Col1':['a','b','c'],
                   'Col2':[{'a':[1,2,3]},{'b':[[2.1],[1]]},{'c':[{'test':'hello'}]}],
                   'Col3':[[[1,2],[3,4]],[[4],[5]],[[3,5,10],[9]]]})
df

输出:

df

    Col1                          Col2               Col3
0      a              {'a': [1, 2, 3]}   [[1, 2], [3, 4]]
1      b           {'b': [[2.1], [1]]}         [[4], [5]]
2      c    {'c': [{'test': 'hello'}]}  [[3, 5, 10], [9]]

如你所见,你可以在 pandas 数据框中拥有列表、列表列表、字典、列表字典、字典列表等。

df.info()

输出:


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   Col1    3 non-null      object
 1   Col2    3 non-null      object
 2   Col3    3 non-null      object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes
type(df.Col3[0])

输出:

list

Yes, you can, here goes an example:

df = pd.DataFrame({'Col1':['a','b','c'],
                   'Col2':[{'a':[1,2,3]},{'b':[[2.1],[1]]},{'c':[{'test':'hello'}]}],
                   'Col3':[[[1,2],[3,4]],[[4],[5]],[[3,5,10],[9]]]})
df

Output:

df

    Col1                          Col2               Col3
0      a              {'a': [1, 2, 3]}   [[1, 2], [3, 4]]
1      b           {'b': [[2.1], [1]]}         [[4], [5]]
2      c    {'c': [{'test': 'hello'}]}  [[3, 5, 10], [9]]

As you can see, you can have lists, lists of lists, dictionaries, dictionaries of lists, lists of dictionaries, etc, inside a pandas dataframe.

df.info()

Output:


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   Col1    3 non-null      object
 1   Col2    3 non-null      object
 2   Col3    3 non-null      object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes
type(df.Col3[0])

Output:

list
凉风有信 2025-01-19 18:27:41

下面是一个使用 h5py 创建 HDF5 文件并加载一些简单数据作为属性的示例。

import h5py
# pixel data to load to a dataset
p = [[1,2],[3,4]]
# attribute names and values:
attr_names = ['POC', "x_position", 'y_position', 'height', 'width']
POC = 10.
x_position = 100
y_position = 200  
height = 16.
width = 5.

with h5py.File('SO_71383695.h5', 'w') as h5w:
    ds = h5w.create_dataset('pixel_data', data=p)
    for name in attr_names:
        ds.attrs[name] = eval(name)
    
with h5py.File('SO_71383695.h5') as h5r:    
    p_data = h5r['pixel_data'][:] # to read into numpy array
    print (p_data)
    for name in h5r['pixel_data'].attrs.keys():
        print(f"{name}: {h5r['pixel_data'].attrs[name]}")

您可以使用 HDFView 查看数据。输出:

[[1 2]
 [3 4]]
POC: 10.0
height: 16.0
width: 5.0
x_position: 100
y_position: 200

    

Here is an example that creactes the HDF5 file with h5py and loads some simple data as attributes.

import h5py
# pixel data to load to a dataset
p = [[1,2],[3,4]]
# attribute names and values:
attr_names = ['POC', "x_position", 'y_position', 'height', 'width']
POC = 10.
x_position = 100
y_position = 200  
height = 16.
width = 5.

with h5py.File('SO_71383695.h5', 'w') as h5w:
    ds = h5w.create_dataset('pixel_data', data=p)
    for name in attr_names:
        ds.attrs[name] = eval(name)
    
with h5py.File('SO_71383695.h5') as h5r:    
    p_data = h5r['pixel_data'][:] # to read into numpy array
    print (p_data)
    for name in h5r['pixel_data'].attrs.keys():
        print(f"{name}: {h5r['pixel_data'].attrs[name]}")

You can view the data with HDFView. Output:

[[1 2]
 [3 4]]
POC: 10.0
height: 16.0
width: 5.0
x_position: 100
y_position: 200

    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文