Reputation: 603
I get returned a list of tuples in this format:
[date1, date2, date3]
[ticker1, ticker2, ticker3]
[value1, value2, value3]
I want to convert it into a pandas dataframe where the index are the dates, columns are the tickers and the data are the values.
Upvotes: 1
Views: 302
Reputation: 323226
Using pivot
df=pd.DataFrame(lst).T
df.pivot(*df)
1 ticker1 ticker2 ticker3
0
date1 value1 None None
date2 None value2 None
date3 None None value3
Upvotes: 3
Reputation: 2190
values = [(1, 0, 1), (2, 9, 4), (3, 10, 4), (9, 22, 30)]
data_frame = pd.DataFrame(values)
print(data_frame)
I would need to see your specific code, because what you outlined appears to be a list of lists, not a list of tuples. But in theory, if you had a list of tuples, this is how you would create a DataFrame out of it. Here is your output:
0 1 2
0 1 0 1
1 2 9 4
2 3 10 4
3 9 22 30
Upvotes: 1
Reputation: 294228
Series
and unstack
lst = [['date1', 'date2', 'date3'],
['ticker1', 'ticker2', 'ticker3'],
['value1', 'value2', 'value3']]
pd.Series(lst[-1], lst[:2]).unstack()
ticker1 ticker2 ticker3
date1 value1 None None
date2 None value2 None
date3 None None value3
defaultdict
from collections import defaultdict
lst = [['date1', 'date2', 'date3'],
['ticker1', 'ticker2', 'ticker3'],
['value1', 'value2', 'value3']]
dd = defaultdict(dict)
for d, t, v in zip(*lst):
dd[t][d] = v
pd.DataFrame(dd)
ticker1 ticker2 ticker3
date1 value1 None None
date2 None value2 None
date3 None None value3
Upvotes: 2