g3nkoVMAESTRO
g3nkoVMAESTRO

Reputation: 25

Swap columns with rows in pandas

I got task to do with quite big frame in pandas. I have downloaded data from climate institution from Poland. I need to extract temperature from couple meteo stations. After some preprocessing in excell and pandas my data is grouped into one DataFrame - code below e.g:

df2= pd.read_excel('./Temperature.xlsx',usecols=['Station name','Year','Month','Day','Deaily mean temperature'])
df2['Date']=pd.to_datetime({'year':df2['Year'],'month':df2['Month'],'day':df2['Day']})
StationGroup = df2.groupby(by='Station name')

Grouping stations into seperate dataframes

Station1=df2.loc[StationGroup.groups['GOLENIÓW']]
Station2=df2.loc[StationGroup.groups['CHRZASTOWO']]
Station3=df2.loc[StationGroup.groups['GDAŃSK-RĘBIECHOWO']]

And then i have concated those stations into one df which is presented in table below:

Stacje=pd.concat(ListaStacji).set_index('Data')
Date Station Name Daily mean temperature
2013-05-01 Station1 -0.5
2013-05-02 Station1 -1
2013-05-03 Station1 -0.7
* *
2013-05-01 Station2 -2.5
2013-05-02 Station2 -1.8
2013-05-03 Station2 -0.3
* *
2013-05-01 Station1 -0.3
2013-05-02 Station1 0.5
2013-05-03 Station1 0.2

I want to have my data in one dataframe but in seperate columns, then i could work on this data e.g:

Date Station1 Station2 Station3
2013-05-01 -0.5 -2.5 0.3
2013-05-02 -1 -1.8 0.5
2013-05-03 -0.7 -0.3 0.2

Thank for any informations and advices

Upvotes: 0

Views: 591

Answers (3)

g3nkoVMAESTRO
g3nkoVMAESTRO

Reputation: 25

Thank you for answers guys. I followed qualitaetsmuell suggestion and i achive what i want. At the beginning i`ve got table like this : FirstVersionOfDataFrame

df.pivot_table(index='Date',columns=['Station name'],values='Daily mean temperature'

so my table is look like i want EndVersionOfDataFrame

Upvotes: 0

qualitaetsmuell
qualitaetsmuell

Reputation: 141

if I understand you correctly, you can use Pandas pivot to get the desired output:

df.pivot(columns="Station Name", values=["Daily mean temperature"], index="Date")["Daily mean temperature"]

Upvotes: 1

Ulewsky
Ulewsky

Reputation: 329

I think you should first groupby stations and dates and then use Stacje.T it will transpose your data frame (change columns with rows)

Upvotes: 0

Related Questions