I'm trying to import data to a pandas DataFrame with columns being date string, label, value. My data looks like the following (just with 4 dates and 5 labels)
from numpy import random import numpy as np import pandas as pd # Creating the data dates = ("2015-01-01", "2015-01-02", "2015-01-03", "2015-01-04") values = [random.rand(5) for _ in range(4)] data = dict(zip(dates,values)) So, the data is a dictionary where the keys are dates, the keys a list of values where the index is the label.
Loading this data structure into a DataFrame
df1 = pd.DataFrame(data) gives me the dates as columns, the label as index, and the value as the value.
An alternative loading would be
df2 = pd.DataFrame() df2.from_dict(data, orient='index') where the dates are index, and columns are labels.
In either of both cases do I manage to do pivoting or stacking to my preferred view.
How should I approach the pivoting/stacking to get the view I want? Or should I change my data structure before loading it into a DataFrame? In particular I'd like to avoid of having to create all the rows of the table beforehand by using a bunch of calls to zip.
pd.DataFrame(data).transpose()meet your needs?from_dictwithorient='index'option.