简体   繁体   中英

Split Panda Column dtype: float64 into several columns

Aim: to create a panda dataframe that can be uploaded to postgresql (I haven't added the pgsql step as it is irrelevant to my question)

Background: I am currently working with a .nc file this is the info:

<type 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    references: Beck, H. E., van Dijk, A. I. J. M., Levizzani, V., Schellekens, J., Miralles, D. G., Martens, B., and de Roo, A.: MSWEP: 3-hourly 0.25 global gridded precipitation (1979-2015) by merging gauge, satellite, and reanalysis data, Hydrol. Earth Syst. Sci. Discuss., doi:10.5194/hess-2016-236
    history: Mon May 15 09:44:10 2017: ncatted -O -a standard_name,Rainf,o,c,rainfall_flux ./3hourly_e2o_netcdf_convention/Rainf_MSWEP_025_197901.nc
    NCO: "4.6.2"
    dimensions(sizes): lon(1440), lat(720), time(249)
    variables(dimensions): float32 lat(lat), float32 lon(lon), float32 time(time), float32 Rainf(time,lat,lon)
    groups: 

I have used xarray to create a pandas dataframe, my code is:

ds = xr.open_dataset(r'.../Rainf_daily_MSWEP_025_197901.nc')
df = ds.to_dataframe()
test =  df.iloc[2:3] # slice the dataframe so that I can see the structure of the column
print test

the output is this:

                                  Rainf
lat     lon      time                    
-89.875 -179.875 1979-01-03  6.705523e-08

As you can see this is a dataframe with one column and at this point I will like to have a dataframe with 4 columns lat, lon, time, Rainf. I have tried str.split, concatenate methods and adding to list and still can't managed to get the columns right. I have also tried using string methods but I have not been able to change the values of the column.

These are some of the lines I have tried

test['Rainf'].astype(str)
test['Rainf'].str.split(' ', 1, expand=True)

I am just after some guidance so any ideas will be welcome. Thank you.

You can reset_index :

In [11]: df
Out[11]:
                                    Rainf
lat     lon      time
-89.875 -179.875 1979-01-03  6.705523e-08

In [12]: df.reset_index()
Out[12]:
      lat      lon        time         Rainf
0 -89.875 -179.875  1979-01-03  6.705523e-08

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM