Jan Jansen
Jan Jansen

Reputation: 53

Xarray.DataSet from a list of Pandas.DataFrames

I have multiple files with bits to analyse. First i read them into a list of BitString.Bits. Then i split each file bits into the specific parts i want to see and save them into a list of Pandas.DataFrames. One DF for each file.

Now for further plotting and analysis purposes i want to store all data in one Xarray.Dataset, where i have the DataFrames stacked along the third axis with the name "dataset".

I have tried to concat each DataFrame together to an DataSet:

xr.concat(data_df[:], dim="dataset")

but i got an error saying that i cant concatenate other than DataArray or DataSets. Can i convert the DataFrames on the fly to DataArrays?

Thanks for your help!

Greetings from Germany

Jan

Upvotes: 5

Views: 2115

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

you can use DataFrame.to_xarray() method:

xr.concat([df.to_xarray() for df in data_df], dim="dataset")

where data_df is a list of DataFrames

Upvotes: 7

Related Questions