带有格式的熊猫组合
Posted
技术标签:
【中文标题】带有格式的熊猫组合【英文标题】:Pandas combinations with format 【发布时间】:2018-08-06 04:10:58 【问题描述】:我的数据集是这样的,
Col1 Col2 Col3
A 10 x1
B 100 x2
C 1000 x3
这就是我希望输出的样子,
Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9
A 10 x1 Empty Empty Empty Empty Empty Empty
B 100 x2 Empty Empty Empty Empty Empty Empty
C 1000 x3 Empty Empty Empty Empty Empty Empty
A 10 x1 B 100 x2 Empty Empty Empty
B 100 x2 C 1000 x3 Empty Empty Empty
A 10 x1 B 100 x2 C 1000 x3
我可以通过 itertools.combinations 获得 A、B、C 的各种组合,但我如何获得这张表?
【问题讨论】:
【参考方案1】:使用 itertools.combinations
和 itertools.chain.from_iterable
:
arr = list(itertools.chain.from_iterable(
[[j for i in el for j in i] for el in itertools.combinations(df.values.tolist(), i)]
for i in range(1, len(df)+1)
)
)
pd.DataFrame(arr)
0 1 2 3 4 5 6 7 8
0 A 10 x1 None NaN None None NaN None
1 B 100 x2 None NaN None None NaN None
2 C 1000 x3 None NaN None None NaN None
3 A 10 x1 B 100.0 x2 None NaN None
4 A 10 x1 C 1000.0 x3 None NaN None
5 B 100 x2 C 1000.0 x3 None NaN None
6 A 10 x1 B 100.0 x2 C 1000.0 x3
另一个使用concat
的选项:
out = pd.concat(
[pd.DataFrame(list(itertools.combinations(df.values.tolist(), i)))
for i in range(1, len(df)+1)]
)
out.applymap(lambda x: [] if type(x) == float else x).sum(1).apply(pd.Series)
0 1 2 3 4 5 6 7 8
0 A 10 x1 NaN NaN NaN NaN NaN NaN
1 B 100 x2 NaN NaN NaN NaN NaN NaN
2 C 1000 x3 NaN NaN NaN NaN NaN NaN
0 A 10 x1 B 100.0 x2 NaN NaN NaN
1 A 10 x1 C 1000.0 x3 NaN NaN NaN
2 B 100 x2 C 1000.0 x3 NaN NaN NaN
0 A 10 x1 B 100.0 x2 C 1000.0 x3
【讨论】:
以上是关于带有格式的熊猫组合的主要内容,如果未能解决你的问题,请参考以下文章