1 回答
TA贡献1806条经验 获得超8个赞
欢迎来到 SO!
我建议使用lambdarow-wise ( axis=1):
from itertools import combinations
import pandas as pd
df = pd.DataFrame({'Asset1':('a','e'), 'Asset2': ('b','f'), 'Asset3': ('c', 'g'), 'Asset4': ('d', 'h')})
df['combinations'] = df.apply(lambda r: list(combinations(r, 3)), axis=1)
print(df)
输出:
Asset1 ... combinations
0 a ... [(a, b, c), (a, b, d), (a, c, d), (b, c, d)]
1 e ... [(e, f, g), (e, f, h), (e, g, h), (f, g, h)]
[2 rows x 5 columns]
list(combinations...如果您稍后只迭代组合,您也可以跳过- 这样您将节省一些内存并将计算延迟到访问的时刻df['combinations']:
df['combinations'] = df.apply(lambda r: combinations(r, 3), axis=1)
print(df)
然后你会在combinations列中得到一个非常神秘的对象:
Asset1 ... combinations
0 a ... <itertools.combinations object at 0x0000022392...
1 e ... <itertools.combinations object at 0x0000022392...
[2 rows x 5 columns]
添加回答
举报