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)

5
  • This is a bit hard for us to debug without more info. Ideally, you could post a full minimal reproducible example but at least could you copy and paste the results of print(data) and print(data_pd) into your question as a text block? Commented Nov 7, 2022 at 1:25
  • Sorry, i just edited the post. I hope it's clearer Commented Nov 7, 2022 at 8:48
  • You can copy the result of print(ds) - it’s a string. Please fully upload images of data/code/logs/etc Commented Nov 7, 2022 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. Commented Nov 8, 2022 at 6:42
  • Does this answer your question? Pandas reset index is not taking effect Commented Nov 8, 2022 at 6:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.