简体   繁体   English

从列表创建熊猫排列的数据框

[英]Create a dataframe of permutations in pandas from lists

I had asked a similar question earlier, but I'm looking for a different output. 我之前曾问过类似的问题,但我正在寻找不同的输出。

Create a dataframe of permutations in pandas from list 从列表创建熊猫排列的数据框

My list is as follows: 我的清单如下:

aa = ['aa1', 'aa2', 'aa3', 'aa4', 'aa5']
bb = ['bb1', 'bb2', 'bb3', 'bb4', 'bb5']
cc = ['cc1', 'cc2', 'cc3', 'cc4', 'cc5']

Now I want to create a dataframe as follows: 现在我要创建一个数据框,如下所示:

aa    bb    cc
aa1   bb1   cc1
aa2   bb1   cc1
aa3   bb1   cc1
aa4   bb1   cc1
aa5   bb1   cc1
aa1   bb2   cc1
aa1   bb3   cc1
aa1   bb4   cc1
aa1   bb5   cc1
aa1   bb1   cc2
aa1   bb1   cc3
aa1   bb1   cc4
aa1   bb1   cc5

The previous suggestion I received was to use: 我收到的先前建议是使用:

lists = [aa, bb, cc]
pd.DataFrame(list(itertools.product(*lists)), columns=['aa', 'bb', 'cc'])

Which gives me a cartesian product. 这给了我笛卡尔积。

But this time, it's not quite what I'm looking for. 但是这次,这并不是我想要的。 I want the output to be exactly like the example output above. 我希望输出与上面的示例输出完全一样。 - So each element in the list, only appears once in each column, except for the first element, which is duplicated to fill the entire column. -因此,列表中的每个元素在每个列中仅出现一次,但第一个元素除外,第一个元素被复制以填充整个列。

Appreciate any help! 感谢任何帮助!

First construct the repeating parts: 首先构造重复部分:

index = pd.RangeIndex(len(aa) + len(bb) + len(cc))
df = pd.DataFrame({'aa':aa[0], 'bb':bb[0], 'cc':cc[0]}, index)

That gives you 15 copies of: 这将为您提供15份副本:

aa1   bb1   cc1

Then overwrite the varying parts: 然后覆盖各个部分:

df.aa[:len(aa)] = aa
df.bb[len(aa):len(aa)+len(bb)] = bb
df.cc[len(aa)+len(bb):] = cc

Which gives the desired output. 给出所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM