Best way to 'unmelt' Pandas output? #5094
Replies: 2 comments 1 reply
-
@stevehadd @hannahbrown7 @DPeterK @pp-mo @nathan962 @stephenworsley how's this: Environment requirements
Code>>> import iris
>>> from iris.pandas import as_data_frame
>>> import pandas as pd
# Activate the new n-dimensional Cube-to-DataFrame conversion.
>>> iris.FUTURE.pandas_ndim = True
# Configure Pandas display.
>>> pd.set_option("display.width", 256)
>>> pd.set_option("display.max_columns", 6)
>>> pd.set_option("display.max_rows", 12)
>>> pd.set_option("display.precision", 3)
>>> path = iris.sample_data_path('ostia_monthly.nc')
>>> cube = iris.load_cube(path)
>>> print(cube)
Here's the 'vanilla' multi-dimensional conversion: >>> df = as_data_frame(cube)
>>> print(df)
An index level can be converted to columns using >>> interest_dim = "latitude"
>>> df_unstacked = df.unstack(level=interest_dim)
>>> print(df_unstacked)
If the row and column headers being >>> df_formatted = df_unstacked.copy(deep=True)
>>> col_index = df_formatted.columns.names.index(interest_dim)
>>> df_formatted.columns = ["lat_{:.1f}".format(c[col_index]) for c in df_formatted.columns]
>>> df_formatted.reset_index(inplace=True)
>>> print(df_formatted)
|
Beta Was this translation helpful? Give feedback.
-
that looks exactly like the sort of thing I was looking for @trexfeathers ! I'm definiitely going to try and update the data prep code use this and see whether there were any any gotchas I've forgotten about. |
Beta Was this translation helpful? Give feedback.
-
Something either for the
iris.pandas
docstring examples, or actual Iris API, depending on how easy it is using Pandas API.Sometimes data scientists find it helpful to specify a dimension of interest for a machine learning model. This should be 'pivoted'/'unmelted' into individual
DataFrame
columns for each dimension value (e.g. 33 columns for 33 height levels).Can this be done by converting the fully melted
DataFrame
thatiris.pandas.to_dataframe()
produces? If that's not easy using Pandas API, what can we do within Iris to aid the conversion between dimensional and tabular data?From @stevehadd and @hannahbrown7
Beta Was this translation helpful? Give feedback.
All reactions