对 Pandas Dataframe 中不同列的多索引 [英] Multiindex to different columns in Pandas Dataframe

查看:107
本文介绍了对 Pandas Dataframe 中不同列的多索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我通过 xray 读入 netCDF 文件后,我得到一个带有 multiindex 的 pandas dataframe> 像这样:

 温度日期经纬度2012-01-01 54 10 20611 20755 10 21011 2142012-01-02 54 10 206……

现在我想将 multiindex 转换成不同的列来进行统计操作.我想得到一个 pd dataframe 像这样:

 temp_54_10 temp_54_11 temp_55_10 temp_55_11日期2012-01-01 206 207 210 2142012-01-02 206 208 213 220……

只有使用 pandas 或 xray 工具才能获得这样的 dataframe,如果是,我该怎么做?

提前致谢:)

附言我使用 python 3.5.0pandas 0.17.0xray 0.6.1

解决方案

尝试拆开.这假设您的数据框名为 df.

<预><代码>>>>df.unstack(['lat', 'lon'])温度纬度 54 5510 11 10 11日期2012-1-1 206 207 210 2142012-1-2 206 NaN NaN NaN

如果你想加入 lat_lon 值,你可以这样做:

df.reset_index(inplace=True)df['lat_lon'] = ["{0}_{1}".format(lat, lon) for lat, lon in zip(df.lat, df.lon)]>>>df.pivot(index='date', columns='lat_lon', values='temp')纬度 54_10 54_11 55_10 55_11日期2012-1-1 206 207 210 2142012-1-2 206 NaN NaN NaN

After I've read in a netCDF file via xray, I get a pandas dataframe with a multiindex like this:

                   temp
date       lat lon                            
2012-01-01 54  10  206
               11  207
           55  10  210
               11  214
2012-01-02 54  10  206
...                ...

Now I want to convert the multiindex into different columns to do statistical manipulations. I want to get a pd dataframe like this:

           temp_54_10 temp_54_11 temp_55_10 temp_55_11
date                                  
2012-01-01 206        207        210        214
2012-01-02 206        208        213        220
...                                         ...

It is possible to get a dataframe like this only with pandas or xray tools and if yes how can I do this?

Thanks in advance :)

p.s. I use python 3.5.0, pandas 0.17.0 and xray 0.6.1

解决方案

Try unstacking. This assumes your dataframe is named df.

>>> df.unstack(['lat', 'lon'])
          temp               
lat         54        55     
lon         10   11   10   11
date                         
2012-1-1   206  207  210  214
2012-1-2   206  NaN  NaN  NaN

If you wish to join the lat_lon values, you can do that as follows:

df.reset_index(inplace=True)
df['lat_lon'] = ["{0}_{1}".format(lat, lon) for lat, lon in zip(df.lat, df.lon)]
>>> df.pivot(index='date', columns='lat_lon', values='temp')
lat_lon   54_10  54_11  55_10  55_11
date                                
2012-1-1    206    207    210    214
2012-1-2    206    NaN    NaN    NaN

这篇关于对 Pandas Dataframe 中不同列的多索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆