title: Transpose a Pandas dataframe around a column with many repeated entries
body: <p>I have created a data frame that has a dozen attributes (e.g. P/E, P/B) for a set of 300 stocks at the end of each month for 10 years. The dataframe is initially indexed by month (so there are 300 observations in each month) and then by stock ticker (so that there are 400 observations associated with each ticker).  My dataframe has  about 12 columns and 120,000 rows. </p>
  
  <p>I now want to transpose the dataframe so that the columns are the stock tickers, i.e. to have about 300 columns, one per stock. I would like the date to continue to be associated with each row, so that I have about 4,800 rows associated with each ticker in a column (12 variables x 400 observations). </p>
  
  <pre><code>#Sort the dataframe, first by ticker and then by date, then write to Excel
  DF.sort_values(by=['Date', 'ticker'], inplace=True)
  DF.to_excel(outPath + outFn1)
  
  #Now make ticker the index and sort by ticker and date 
  DF = DF.reset_index().set_index('ticker')
  DF.sort_values(by=['ticker', 'Date'], inplace=True)
  DF.to_excel(outPath + outFn2)
  
  #Now transpose the dataframe
  DF = DF.reset_index().set_index('Date')
  DF = DF.transpose()
  DF.to_excel(outPath + outFn3)
  </code></pre>
  
  <p>Unfortunately, when I do the last df.transpose(), I get 120,000 columns and only 12 rows – the 300 tickers and 400 months are transformed into 120,000 columns, with the month (the index) being displayed in the first row! Is there a way to have only 300 columns (one per ticker) and 4,800 rows (one per month per variable)?</p>
  
  <p>Many thanks in advance for your assistance</p>
  
  <p>Thomas Philips</p>