pandas 从另一个数组给予数组元素标签

bmp9r5qi  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(113)

我有两个数组:
data有300个数组,每个数组有31个值,char有300个数组,每个数组有1个值。
什么是最好的方法来合并这两个在这种形式:
x[0] = [{char[0], data[0]; char[1], data[1]; char[n], data[n]; char[299], data[299]}]
我需要它来使用Pandas剖析器找到相关性,从数据到他们指定的字符。
泰姆!

6gpjuf90

6gpjuf901#

您可以执行以下操作:

import pandas as pd

# Example data and char arrays
data = [[1, 2, 3] for i in range(300)]
char = [i for i in range(300)]

combined = list(zip(char, data))

grouped = {}
for c, d in combined:
    if c not in grouped:
        grouped[c] = []
    grouped[c].append(d)

dict_list = []
for c in range(300):
    dict_list.append({'char': c, 'data': grouped[c]})

# Assign the list of dictionaries to x[0]
x = [dict_list]
values = [[d['char']] + d['data'] for d in dict_list]

# Assign the values to x[0]
x = [values]

其给出:

[(0, [1, 2, 3]),
 (1, [1, 2, 3]),
 (2, [1, 2, 3]),
 (3, [1, 2, 3]),
 (4, [1, 2, 3]),
 (5, [1, 2, 3]),
 (6, [1, 2, 3]),
 (7, [1, 2, 3]),
 (8, [1, 2, 3]),
 (9, [1, 2, 3]),
 (10, [1, 2, 3]),
 (11, [1, 2, 3]),
 (12, [1, 2, 3]),
 (13, [1, 2, 3]),
 (14, [1, 2, 3]),
 (15, [1, 2, 3]),
 (16, [1, 2, 3]),
 (17, [1, 2, 3]),
 (18, [1, 2, 3]),
 (19, [1, 2, 3]),
 (20, [1, 2, 3]),
 (21, [1, 2, 3]),
 (22, [1, 2, 3]),
 (23, [1, 2, 3]),
 (24, [1, 2, 3]),
 (25, [1, 2, 3]),
 (26, [1, 2, 3]),
 (27, [1, 2, 3]),
 (28, [1, 2, 3]),
 (29, [1, 2, 3]),
 (30, [1, 2, 3]),
 (31, [1, 2, 3]),
 (32, [1, 2, 3]),
 (33, [1, 2, 3]),
 (34, [1, 2, 3]),
 (35, [1, 2, 3]),
 (36, [1, 2, 3]),
 (37, [1, 2, 3]),
 (38, [1, 2, 3]),
 (39, [1, 2, 3]),
 (40, [1, 2, 3])
....
(293, [1, 2, 3]),
 (294, [1, 2, 3]),
 (295, [1, 2, 3]),
 (296, [1, 2, 3]),
 (297, [1, 2, 3]),
 (298, [1, 2, 3]),
 (299, [1, 2, 3])]

相关问题