【发布时间】:2020-07-11 21:23:55
【问题描述】:
我有一个这样的数据框:
id a b c d e
0 a10 a11 a12 a13 a14
1 a10 a21 a12 a23 a24
2 a30 a21 a12 a33 a14
3 a30 a21 a12 a43 a44
4 a10 a51 a12 a53 a14
我想要数据框中长度为“x”的所有唯一组合列表。如果长度为 3,那么一些组合将是:
[[a10,a11,a12],[a10,a21,a12],[a10,a51,a12],[a30,a11,a12],[a30,a21,a12],[a30,a51,a12],
[a11,a12,a13],[a11,a12,a23],[a11,a12,a33],[a11,a12,a43],[a11,a12,a53],[a21,a12,a13]....]
只有两个约束:
1. Length of combination lists should be equal to the 'x'
2. In one combination, there can be at max only 1 unique value from a column of dataframe.
下面给出了构建数据框的最少代码。任何帮助都感激不尽。谢谢!
data_dict={'a':['a10','a10','a30','a30','a10'],
'b':['a11','a21','a21','a21','a51'],
'c':['a12','a12','a12','a12','a12'],
'd':['a13','a23','a33','a43','a53'],
'e':['a14','a24','a14','a44','a14']}
df1=pd.DataFrame(data_dict)
【问题讨论】:
标签: python-3.x pandas dataframe combinations