I have a dataframe with columns that include latitude, longitude, time, and data values. I would like to reshape it and transform it into an xarray dataarray such that the dimensions are time x lat/long pair but am not sure of the most efficient way to do this.
To make it concrete, the dataframe is structured as follows:
Index Latitude Longitude Time Data
0 1 2 1 1
1 2 4 1 2
2 1 2 2 3
I want the data to be reshaped such that it ends up as a matrix:
Latitude 1/Longitude 2 Latitude 2/Longitude 4
Time 1 1 2
Time 2 3 Null
I’m currently doing this by taking a for loop over the unique lat/long combinations, saving each as an xarray, and then concatenating them over the lat/long dimension.
Are there any ways to more efficiency reshape the data?
Pivot is what you want, but first you need the new column names:
df['col'] = 'Latitude' + df['Latitude'].astype(str) + '/Longitude' + df.Longitude.astype(str)
df.pivot(index='Time', columns='col', values='Data')
Output:
col Latitude1/Longitude2 Latitude2/Longitude4
Time
1 1.0 2.0
2 3.0 NaN
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments