Create a dataframe of pairs

Hi,

I created pairs in the "Unsupervised Learning in Trading" course, like this list:

https://drive.google.com/file/d/1uc-1x9EK9AQaJRsbJ6HPnxR8ft8QxLuW/view?usp=share_link

Now I want to run the cointegration test, but I have to convert the list in a dataframe like this one:

https://drive.google.com/file/d/1tNTIk1WmxgNTCn-JQmcemvjzLxE-uqvN/view?usp=share_link

How can I do it?

Hi Alessandro,



You can use the following lines of code to convert the same:

pairs_df = stocks_pair.explode("Pairs").reset_index()
pairs_df[["Stock1", "Stock2"]] = pd.DataFrame(pairs_df["Pairs"].tolist())
pairs_df = pairs_df[["Stock1", "Stock2"]]

Hope this helps!



Thanks,

Akshay