简体   繁体   English

将 Panda Column dtype: float64 拆分为几列

[英]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)目标:创建一个可以上传到 postgresql 的熊猫数据框(我没有添加 pgsql 步骤,因为它与我的问题无关)

Background: I am currently working with a .nc file this is the info:背景:我目前正在处理一个 .nc 文件,这是信息:

<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:我使用 xarray 创建了一个熊猫数据框,我的代码是:

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.如您所见,这是一个只有一列的数据框,此时我想要一个包含 4 列 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.我已经尝试过 str.split、连接方法和添加到列表中,但仍然无法使列正确。 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 :您可以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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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