我想知道是否有一種更有效的方法可以從 Python 中的組合物件創建資料框。例如:
我有一個清單:
lst = [1, 2, 3, 4, 5]和pick_size = 3
combos_obj = itertools.combinations(lst, pick_size)
到目前為止,我一直在使用串列理解:
combos = [list(i) for i in combos_obj]
df = pd.DataFrame(combos)
這給了我想要的結果,但是有沒有更有效的方法來做到這一點?
uj5u.com熱心網友回復:
只需直接從itertools.combinations回傳的生成器創建一個資料框:
>>> combos_obj = itertools.combinations(lst, pick_size)
>>> combos_obj
<itertools.combinations at 0x12ba9d220>
>>> df = pd.DataFrame(combos_obj)
>>> df
0 1 2
0 1 2 3
1 1 2 4
2 1 2 5
3 1 3 4
4 1 3 5
5 1 4 5
6 2 3 4
7 2 3 5
8 2 4 5
9 3 4 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486105.html
上一篇:基于組和條件的新列
