Lucas P.
Lucas P.

Reputation: 4532

Transform multiple CSV rows in Pandas into one

I'm just starting out with Pandas and I'm trying to make a data file I have into something I can export and read. The CSV I have is in this form:

time    |   parameter   |   value
------------------------------------
1       |       a       |   21
2       |       a       |   21
3       |       a       |   21
1       |       b       |   19
2       |       b       |   19
3       |       b       |   19
1       |       c       |   17
2       |       c       |   17
3       |       c       |   17

I want to transform it in the following form:

time    |   a   |   b   |   c   
------------------------------------
1       |   21  |   19  |   17  
2       |   21  |   19  |   17  
3       |   21  |   19  |   17  
1       |   21  |   19  |   17  
2       |   21  |   19  |   17  
3       |   21  |   19  |   17  
1       |   21  |   19  |   17  
2       |   21  |   19  |   17  
3       |   21  |   19  |   17  

Of course my data have different values, but the example above should be sufficient. It's weather data, like temperature and wind speed, and each row has the timestamp of the measurement, the param name and the value.

I want to transform it into a single row with 3 columns (or more if there are more parameters) for each timestamp, where the column name is the param name.

I know that I have to group my data by the time column so I've done df.groupby('time')

However, I cannot figure out how to execute an apply method that will give me the results I want. Any hints are appreciated!

Upvotes: 2

Views: 78

Answers (1)

ayorgo
ayorgo

Reputation: 3902

You can try using pivot table:

pd.pivot_table(df, index='time', columns='parameter', values='value')

parameter   a   b   c
time                 
1          21  19  17
2          21  19  17
3          21  19  17

Upvotes: 1

Related Questions