Skip to content
Advertisement

set an index while merging two dataframe

I have a dataframe like this :

dte res year
1995-01-01 65.3 1995
1995-01-02 65.5 1995
2019-01-03 55.2 2019
2019-01-04 52.2 2019

and I’m trying to create another file in this format :

JavaScript

basically I want every year in a different column.

Here is what I already did :

JavaScript

when I write in my loop df=pd.DataFrame(myDict,index=[row.dte]) and df2=pd.DataFrame(myDict,index=[row.dte]) it doesn’t work at all. so i tried : df=pd.DataFrame(myDict,index=[output]) and df2=pd.DataFrame(myDict,index=[output]) but my years are still on the same column as my first table.

I already checked, df and df2 seems fine, so maybe my issue is in my .append ?

I know it’s about the index, but i have no idea how to fix it, i don’t understand well how it works. Can you help me ?

thank you.

Advertisement

Answer

set a custom index then use unstack – we can then flatten the subsequent multi index.

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement