簡體   English   中英

將 Panda Column dtype: float64 拆分為幾列

[英]Split Panda Column dtype: float64 into several columns

目標:創建一個可以上傳到 postgresql 的熊貓數據框(我沒有添加 pgsql 步驟,因為它與我的問題無關)

背景:我目前正在處理一個 .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: 

我使用 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

輸出是這樣的:

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

如您所見,這是一個只有一列的數據框,此時我想要一個包含 4 列 lat、lon、time、Rainf 的數據框。 我已經嘗試過 str.split、連接方法和添加到列表中,但仍然無法使列正確。 我也嘗試過使用字符串方法,但我無法更改列的值。

這些是我嘗試過的一些線路

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

我只是在接受一些指導,因此歡迎提出任何想法。 謝謝你。

您可以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