List of Tuples to DataFrame Conversion

PythonListPandasTuples

Python Problem Overview


I have a list of tuples similar to the below:

[(date1, ticker1, value1),(date1, ticker1, value2),(date1, ticker1, value3)]

I want to convert this to a DataFrame with index=date1, columns=ticker1, and values = values. What is the best way to do this?

EDIT:

My end goal is to create a DataFrame with a datetimeindex equal to date1 with values in a column labeled 'ticker':

df = pd.DataFrame(tuples, index=date1)

Right now the tuple is generated with the following:

tuples=list(zip(*prc_path))

where prc_path is a numpy.ndarray with shape (1000,1)

Python Solutions


Solution 1 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmolivizzyView Question on Stackoverflow
Solution 1 - PythonelyaseView Answer on Stackoverflow