List of Tuples to DataFrame Conversion [duplicate]

I think this is what you want:

>>> data = [('2013-01-16', 'AAPL', 1),
            ('2013-01-16', 'GOOG', 1.5),
            ('2013-01-17', 'GOOG', 2),
            ('2013-01-17', 'MSFT', 4),
            ('2013-01-18', 'GOOG', 3),
            ('2013-01-18', 'MSFT', 3)]

>>> df = pd.DataFrame(data, columns=['date', 'ticker', 'value'])
>>> df
         date ticker  value
0  2013-01-16   AAPL    1.0
1  2013-01-16   GOOG    1.5
2  2013-01-17   GOOG    2.0
3  2013-01-17   MSFT    4.0
4  2013-01-18   GOOG    3.0
5  2013-01-18   MSFT    3.0

>>> df.pivot('date', 'ticker', 'value')
ticker      AAPL  GOOG  MSFT
date                        
2013-01-16     1   1.5   NaN
2013-01-17   NaN   2.0     4
2013-01-18   NaN   3.0     3

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)