我有一个巨大的excel文件,其中包含以下数据:
DATE OIL GAS WATER OIL GAS WATER
01-01-2018 100 200 300 10 20 30
01-02-2018 200 500 300 30 40 10
01-03-2018 10 50 30 20 40 10
...
如何使用python将其转换为以下格式:
DATE OIL GAS WATER
01-01-2018 100 200 300
01-02-2018 200 500 300
01-03-2018 10 50 30
01-01-2018 10 20 30
01-02-2018 30 40 10
01-03-2018 20 40 10
答案 0 :(得分:0)
您可以连续使用pd.melt(Dataframe,...)
和concat
df
( Dataframe )列:
df1 = pd.melt(df,id_vars=['Date'],value_vars=['Oil','Oil.1'], value_name='Oil')
df2 = pd.melt(df,id_vars=['Date'],value_vars=['Gas','Gas.1'], value_name='Gas')
df3 = pd.melt(df,id_vars=['Date'],value_vars=['Water','Water.1'], value_name='Water')
pd.concat([df1.Date,df1.Oil,df2.Gas,df3.Water],axis=1)