Home > Back-end >  Cross terms based columns in dataframe
Cross terms based columns in dataframe

Time:09-23

How should I generate the data2 from data1?
The rows of data2 should be the corss terms of data1.
Thanks in advance!

data1 = pd.DataFrame([('1M, 6M, 1Y'), ('1M, 6M, 1Y')])
            0
0  1M, 6M, 1Y
1  1M, 6M, 1Y
data2 = pd.DataFrame([('1M', '1M'),
                     ('1M', '6M'),
                     ('1M', '1Y'),
                     ('6M', '1M'),
                     ('6M', '6M'),
                     ('6M', '1Y'),
                     ('1Y', '1M'),
                     ('1Y', '6M'),
                     ('1Y', '1Y'),])

    0   1
0  1M  1M
1  1M  6M
2  1M  1Y
3  6M  1M
4  6M  6M
5  6M  1Y
6  1Y  1M
7  1Y  6M
8  1Y  1Y

CodePudding user response:

this should work

index_s0 = pd.MultiIndex.from_product([data1[0][0].split(','), data1[0][0].split(',')], names=["0", "1"])
so_df = pd.DataFrame(index=index_s0).reset_index()


out:
     0    1
0   1M   1M
1   1M   6M
2   1M   1Y
3   6M   1M
4   6M   6M
5   6M   1Y
6   1Y   1M
7   1Y   6M
8   1Y   1Y

CodePudding user response:

You can use itertools.product:

from itertools import product

out = pd.DataFrame(product(*(x.split(', ') for x in data1[0])),
                   columns=data1.index) # optional

NB. this should work with any number of rows.

output:

    0   1
0  1M  1M
1  1M  6M
2  1M  1Y
3  6M  1M
4  6M  6M
5  6M  1Y
6  1Y  1M
7  1Y  6M
8  1Y  1Y
  • Related