1

So I have the following code as an exercise and I am trying to melt and unmelt the table using pivot. These are what I have so far:

sp500data=yf.Tickers('MMM ABT ...ZTS).history(group_by='ticker',period = '2y')

melted = sp500data.reset_index().melt(id_vars=['Date'], value_name='Value').rename(columns={'variable_0':'Ticker','variable_1':'Attributes' })

pivoted = melted.pivot(index='Date', columns='Attributes')

I get error whenever I try to display pivoted('ValueError: Index contains duplicate entries, cannot reshape'). Please help

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 1
    Oh I see I should assign my columns as a list['Ticker', 'Attribute']. That seems to solve my problem for now. I might end up coming to see responses if I get stuck again –  Jun 14 '21 at 15:32
  • 1
    If you have solved your problem, please considering posting your comment as an answer - that way people with the same question can use your answer (most of the time people don't check comments) – Derek O Jun 14 '21 at 16:20

1 Answers1

0

Oh I see I should assign my columns as a list['Ticker', 'Attribute']. That seems to solve my problem for now