0

I have a problem converting a Xarray dataset into a Pandas dataframe. The thing is that when I convert into a pd dataframe, pandas don't see the right amount of columns. During the conversion I switch from 5 columns (lat, lon, time, sst, time_bnds) to only 2 columns.

Here is my code

PATH = os.path.join (DIR, 'sst.mnmean.nc')
data=xr.open_dataset(PATH)

I'm not able to copy paste the result so here is a screenshot

data_pd = data.to_dataframe(dim_order=None)

Here is the result in pandas dataframe

As you can see pandas sees only 2 columns even though there are 6 variables.

I was expecting to see the 5 exact same columns in my pandas dataframe, which is not the case. To be clear, I just want to work with sst variable (sea surface temperature) and time bnds which is the timestamp

(I know that coordinates are considered as index, I tried reset_index() but it doesn't work)

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
NonoVoyou
  • 21
  • 2
  • This is a bit hard for us to debug without more info. Ideally, you could post a full [mre] but at least could you copy and paste the results of `print(data)` and `print(data_pd)` into your question as a text block? – Michael Delgado Nov 07 '22 at 01:25
  • Sorry, i just edited the post. I hope it's clearer – NonoVoyou Nov 07 '22 at 08:48
  • You can copy the result of print(ds) - it’s a string. Please fully upload images of data/code/logs/etc – Michael Delgado Nov 07 '22 at 15:35
  • reset_index does work, but it's not an inplace operation, so try `data_pd = data_pd.reset_index()`. the other variables are just in the index - they're not columns. – Michael Delgado Nov 08 '22 at 06:42
  • Does this answer your question? [Pandas reset index is not taking effect](https://stackoverflow.com/questions/27736267/pandas-reset-index-is-not-taking-effect) – Michael Delgado Nov 08 '22 at 06:43

0 Answers0