Python Panel Data
I am usually using Stata but now want to use Python and desperately trying to create a pandel data set. I tried pandas.panel but do not get it to work. I have the following datase
Solution 1:
Given input data:
data = [
{"date": 2000, "id1": 100, "id2": 50},
{"date": 2001, "id1": 101, "id2": 48}
]
or
data= {
"date": [2000, 2001],
"id1": [100, 101],
"id2": [50, 48],
}
such that
df = pd.DataFrame(data)
df
"melt" the pandas DataFrame:
melted = pd.melt(df, id_vars="date", var_name="id", value_name="variable")
# Optional amendments
melted["id"] = melted["id"].str.replace("id", "")
melted.sort_values(by="date", inplace=True)
melted.reset_index(inplace=True, drop=True)
melted
melted
Output
Additional Reference: Wickham, H. Tidy Data, The Journal of Statistical Software, 10, 59, 2014.
Post a Comment for "Python Panel Data"