我在Pandas中构建3D DataFrame时遇到了困难。我想要这样的东西
A B C
start end start end start end ...
7 20 42 52 90 101
11 21 213 34
56 74 9 45
45 12
哪里 A
, B
等等是顶级描述符和 start
和 end
是次要描述。随后的数字是成对的,并且没有相同数量的对 A
, B
等等 A
有四个这样的对, B
只有1,和 C
有3个。
我不知道如何继续构建这个DataFrame。修改 这个 示例没有给我设计输出:
import numpy as np
import pandas as pd
A = np.array(['one', 'one', 'two', 'two', 'three', 'three'])
B = np.array(['start', 'end']*3)
C = [np.random.randint(10, 99, 6)]*6
df = pd.DataFrame(zip(A, B, C), columns=['A', 'B', 'C'])
df.set_index(['A', 'B'], inplace=True)
df
产生:
C
A B
one start [22, 19, 16, 20, 63, 54]
end [22, 19, 16, 20, 63, 54]
two start [22, 19, 16, 20, 63, 54]
end [22, 19, 16, 20, 63, 54]
three start [22, 19, 16, 20, 63, 54]
end [22, 19, 16, 20, 63, 54]
有没有办法将C中的列表分解为自己的列?
编辑:我的结构 C
很重要它看起来如下:
C = [[7,11,56,45], [20,21,74,12], [42], [52], [90,213,9], [101, 34, 45]]
并且所需的输出是顶部的输出。它代表某个序列中子序列的起点和终点(A
, B
。 C
是不同的序列)。根据序列本身,有不同数量的子序列满足我正在寻找的给定条件。因此,有不同数量的start:end对 A
, B
等等