I have a dataframe where I need to divide the sensor data into 3 different columns (one for each type of sensor data), and they must match by date.
The following image is how I currently have the df:
The following image is a sample of the output of the df I need:
I attach a link to the original dataframe:
https://drive.google.com/file/d/1-5X1lK8yYkYrS9-2myEa_-njtsdt57cN/view?usp=sharing
I've tried to transpose, but I'm not sure how to do it.
df = df.pivot(index='fechaValor', columns='sensorica', values='Valor')
Thanks in advance.
All the best.
Good day,
You almost made it, there are several ways to do it:
Note: The URL you put in doesn't seem to work, using the following
dataframe
generic in a "sample2.csv" fileUsing
pandas.pivot
:When printing
df1
we get the followingdataframe
:Using
pandas.pivot_table
:It would be exactly the same but using another function:
using
pandas.crosstab
Using
pandas.DataFrame.groupby
:A disadvantage of this method is that when using groupby, multiple levels of columns are created, so we have to use
reset_index
thedataframe
transpose (Conpandas.DataFrame.T
) and then transpose it again, at the end we must give each column a name.additional note
I recommend you try all the methods (there are more ways to do it but the answer would be too big) and execute them line by line, also read the documentation of each one to see the difference, advantages and/or disadvantages.
For all the examples the result is the same as the first example but I omitted them to make the answer shorter.