How to change rows to columns by date in pandas?
<p>I have a csv data file in following format, I want to change the rows to columns , but this conversion needs to be done per stock and per date.</p>

<pre><code>Ticker,Indicator,Date,Value
STOCK A,ACCRUALS,3/31/2005,-10.44
STOCK A,ACCRUALS,3/31/2006,0.44
STOCK A,AE,3/31/2005,3.97
STOCK A,AE,3/31/2006,3.67
STOCK A,ASETTO,3/31/2005,0.762
STOCK A,ASETTO,3/31/2006,0.9099
</code></pre>

<p>Output</p>

<pre><code>Ticker,Date,ACCRUALS,AE,ASETTO
STOCK A,3/31/2005,-10.44,3.97,0.762
STOCK A,3/31/2006,0.44,3.67,0.9099
</code></pre>

<pre><code>Ticker,Indicator,Date,Value
STOCK A,ACCRUALS,3/31/2005,-10.44
STOCK A,ACCRUALS,3/31/2006,0.44
STOCK A,AE,3/31/2005,3.97
STOCK A,AE,3/31/2006,3.67
STOCK A,ASETTO,3/31/2005,0.762
STOCK A,ASETTO,3/31/2006,0.9099
</code></pre>

<p>Let's just say your data are in a dataframe called <code>df</code>:</p>

<pre><code>&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; df = df.set_index(df['Date'])
&gt;&gt;&gt; for ind in set(df['Indicator']):
...     filtered_df = df[df['Indicator'] == ind]
...     df[ind] = filtered_df['Value']
... 
&gt;&gt;&gt; cols_to_keep = ['Ticker', 'Date'] + list(set(df['Indicator']))
&gt;&gt;&gt; trimmed_df = df[cols_to_keep]
&gt;&gt;&gt; trimmed_df = trimmed_df.drop_duplicates()
&gt;&gt;&gt; trimmed_df
            Ticker       Date  ACCRUALS    AE  ASETTO
Date                                                 
3/31/2005  STOCK A  3/31/2005    -10.44  3.97  0.7620
3/31/2006  STOCK A  3/31/2006      0.44  3.67  0.9099
</code></pre>

<p>That should take each unique value for <code>df['Indicator']</code> and make a new column out of the <code>df['Value']</code> column for that particular indicator.</p>

<p>You can use <code>reset_index()</code> to set the dataframe's indices back to zero:</p>

<pre><code>&gt;&gt;&gt; trimmed_df.reset_index(drop = True)
</code></pre>

<p>And, instead of using <code>cols_to_keep</code>, you can do:</p>

<pre><code>&gt;&gt;&gt; trimmed_df.drop("Indicator", axis = 1, inplace = True)
</code></pre>

python pandas