Python 3.x 使用Python在一行中进行组合
我的数据看起来像这样Python 3.x 使用Python在一行中进行组合,python-3.x,pandas,csv,merge,Python 3.x,Pandas,Csv,Merge,我的数据看起来像这样 Trade -- Strategy -- -- Maturity Swap -- OMASW28 -- 10 -- 4/5/2028 Bond -- OMASW28 -- 20 -- 4/5/2028 Bond -- OMASW28 -- 30 -- 4/5/2028 Trade -- Strategy -- Totals -- Maturity -- Trade -- Strategy -- Totals -- Trade -- Strategy -- Tota
Trade -- Strategy -- -- Maturity
Swap -- OMASW28 -- 10 -- 4/5/2028
Bond -- OMASW28 -- 20 -- 4/5/2028
Bond -- OMASW28 -- 30 -- 4/5/2028
Trade -- Strategy -- Totals -- Maturity -- Trade -- Strategy -- Totals -- Trade -- Strategy -- Totals
Swap -- OMASW28 -- 10 -- 4/5/2028 -- Bond -- OMASW28 -- 20 -- 4/5/2028 --
Bond -- OMASW28 -- 30 -- 4/5/2028
主要目标是找出所有可能的组合及其累积总数,例如,我希望最终的表如下所示
Trade -- Strategy -- -- Maturity
Swap -- OMASW28 -- 10 -- 4/5/2028
Bond -- OMASW28 -- 20 -- 4/5/2028
Bond -- OMASW28 -- 30 -- 4/5/2028
Trade -- Strategy -- Totals -- Maturity -- Trade -- Strategy -- Totals -- Trade -- Strategy -- Totals
Swap -- OMASW28 -- 10 -- 4/5/2028 -- Bond -- OMASW28 -- 20 -- 4/5/2028 --
Bond -- OMASW28 -- 30 -- 4/5/2028
在每一行的末尾,有一个字段,其中包含总计,在本例中为10+20+30=60
我已经讲到这里了,但这似乎太长太复杂了
Trade_x Strategy Nominal_x Total_x Maturity_x Trade_y Nominal_y Total_y Maturity_y
Swap OMASW28 -20000000 10 4/5/2028 Swap -20000000 10 4/5/2028
Swap OMASW28 -20000000 10 4/5/2028 Bond 20000000 20 4/5/2028
Swap OMASW28 -20000000 10 4/5/2028 Bond 1000 30 4/5/2028
Bond OMASW28 20000000 20 4/5/2028 Swap -20000000 10 4/5/2028
Bond OMASW28 20000000 20 4/5/2028 Bond 20000000 20 4/5/2028
Bond OMASW28 20000000 20 4/5/2028 Bond 1000 30 4/5/2028
Bond OMASW28 1000 30 4/5/2028 Swap -20000000 10 4/5/2028
Bond OMASW28 1000 30 4/5/2028 Bond 20000000 20 4/5/2028
Bond OMASW28 1000 30 4/5/2028 Bond 1000 30 4/5/2028
基本上是以下代码
a = pd.read_csv('files/input.csv')
b = pd.read_csv('files/input.csv')
merged = a.merge(b, on='Strategy')
这个合并两次,对于这个特殊情况我需要三次,如果我有10行,每行4列,我希望最后的表是40列宽!
有什么想法吗 这是两次是什么意思?我是说它合并了一次,与所有其他行合并。现在我有了所有可能的两行的比较。我希望合并,这样我就可以有所有三行的所有可能的组合,最初出现!