How to reshape pandas dataframe by column values?

Lauren K

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?

Quang Hoang

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas DataFrame reshape by multiple column values

From Dev

How to reshape dataframe with pandas

From Dev

How to reshape a pandas DataFrame?

From Dev

How to reshape dataframe with pandas?

From Dev

Pandas reshape dataframe values as columns

From Dev

Python Pandas : How to reshape the dataframe, specific cell to new column

From Dev

Pandas how to reshape a dataframe containing duplicated values for columns

From Dev

reshape Pandas dataframe by appending column to column

From Dev

How to reshape pandas dataframe with pivot?

From Dev

how to reshape/ explode pandas dataframe?

From Dev

how to reshape dataframe in python pandas

From Dev

Reshape the Pandas dataframe based on a single column

From Python

Reshape Pandas Dataframe with multiple column groups

From Dev

Pandas reshape Dataframe based on column value

From Dev

Reshape Pandas dataframe based on values in two columns

From Dev

How to use column values as headers in Pandas DataFrame

From Java

How to select a range of values in a pandas dataframe column?

From Dev

How to iterate and edit values of a column in pandas dataframe

From Python

How to switch column values in the same Pandas DataFrame

From Dev

How to edit all values of a column in a pandas dataframe?

From Dev

How to Split the values of a column in a Pandas dataframe?

From Dev

How to add a column in a pandas dataframe with values that repeat?

From Dev

How to add values to a new column in pandas dataframe?

From Dev

How to slice column values in Python pandas DataFrame

From Dev

How to join column values in pandas MultiIndex DataFrame?

From Dev

How to calculate ratio of values in a pandas dataframe column?

From Dev

How to check a type of column values in pandas DataFrame

From Dev

How to take values in the column as the columns in the DataFrame in pandas

From Dev

How extract values of dictionary column in pandas dataframe

Related Related

  1. 1

    pandas DataFrame reshape by multiple column values

  2. 2

    How to reshape dataframe with pandas

  3. 3

    How to reshape a pandas DataFrame?

  4. 4

    How to reshape dataframe with pandas?

  5. 5

    Pandas reshape dataframe values as columns

  6. 6

    Python Pandas : How to reshape the dataframe, specific cell to new column

  7. 7

    Pandas how to reshape a dataframe containing duplicated values for columns

  8. 8

    reshape Pandas dataframe by appending column to column

  9. 9

    How to reshape pandas dataframe with pivot?

  10. 10

    how to reshape/ explode pandas dataframe?

  11. 11

    how to reshape dataframe in python pandas

  12. 12

    Reshape the Pandas dataframe based on a single column

  13. 13

    Reshape Pandas Dataframe with multiple column groups

  14. 14

    Pandas reshape Dataframe based on column value

  15. 15

    Reshape Pandas dataframe based on values in two columns

  16. 16

    How to use column values as headers in Pandas DataFrame

  17. 17

    How to select a range of values in a pandas dataframe column?

  18. 18

    How to iterate and edit values of a column in pandas dataframe

  19. 19

    How to switch column values in the same Pandas DataFrame

  20. 20

    How to edit all values of a column in a pandas dataframe?

  21. 21

    How to Split the values of a column in a Pandas dataframe?

  22. 22

    How to add a column in a pandas dataframe with values that repeat?

  23. 23

    How to add values to a new column in pandas dataframe?

  24. 24

    How to slice column values in Python pandas DataFrame

  25. 25

    How to join column values in pandas MultiIndex DataFrame?

  26. 26

    How to calculate ratio of values in a pandas dataframe column?

  27. 27

    How to check a type of column values in pandas DataFrame

  28. 28

    How to take values in the column as the columns in the DataFrame in pandas

  29. 29

    How extract values of dictionary column in pandas dataframe

HotTag

Archive