Skip to content

Convert a dataframe of all float columns to a 2D xarray? #7526

Answered by jhamman
johann-petrak asked this question in Q&A
Discussion options

You must be logged in to vote

You can simply pass a Dataframe to the DataArray constructor:

import xarray as xr
import sklearn.datasets as skd 

xdf, ydf = skd.load_iris(return_X_y=True, as_frame=True)
da = xr.DataArray(xdf)

(note: this does drop the name of the index dimensions)

Or you can convert the dataframe to a Dataset then to a DataArray:

xdf.to_xarray().to_array()

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@johann-petrak
Comment options

@keewis
Comment options

Comment options

You must be logged in to vote
1 reply
@johann-petrak
Comment options

Answer selected by johann-petrak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants