【发布时间】:2014-08-09 00:58:52
【问题描述】:
我在 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。修改 this 示例并没有给我设计的输出:
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 是不同的序列)。根据序列本身,有不同数量的子序列满足我正在寻找的给定条件。因此,A、B 等的 start:end 对的数量不同
【问题讨论】: