Pandas DataFrame:使用列的唯一值转换帧 [英] Pandas DataFrame: transforming frame using unique values of a column

查看:215
本文介绍了Pandas DataFrame:使用列的唯一值转换帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas dataframe / csv格式

 日期国家类型Val 
2013-01-01美国x 23
2013-01-01美国y 13
2013-01-01 MX x 11
2013-01-01 MX y 14
2013-01-02美国x 20
2013-01-02美国y 19
2013-01-02 MX x 14
2013-01-02 MX y 16

我想将其转换为表单

  xy 
2013-01-01美国23 13
2013-01-01 MX 11 14
2013-01-02美国20 19
2013-01-02 MX 14 16

一般来说,我正在寻找一种使用单列的唯一值来转换表的方法。 p>

我已经看过 pivot groupby 得到确切的形式。



提示:可能这是可以通过 pivot 解决,但我还是无法得到

解决方案

可能不是最优雅的方式,但使用取消堆叠

 >>>> df 
date国家类型Val
0 2013-01-01美国x 23
1 2013-01-01美国y 13
2 2013-01-01 MX x 11
3 2013-01-01 MX y 14
4 2013-01-02美国x 20
5 2013-01-02美国y 19
6 2013-01-02 MX x 14
7 2013-01-02 MX y 16

>>>> df.set_index(['date','Country','Type'])undack('Type')。reset_index()
date国家Val
类型xy
0 2013-01 -01 MX 11 14
1 2013-01-01美国23 13
2 2013-01-02 MX 14 16
3 2013-01-02美国20 19

一般来说,删除结果中奇怪的分层列:

 >>> cols = [c for d in df.columns if c not in {'Type','Val'}] 
>>>> df2 = df.set_index(cols + ['Type'])undack('Type')
>>> df2
Val
类型xy
日期国家
2013-01-01 MX 11 14
美国23 13
2013-01-02 MX 14 16
USA 20 19
>>>> df2.columns = df2.columns.levels [1]
>>>> df2.columns.name = None
>>>> df2
xy
日期国家
2013-01-01 MX 11 14
美国23 13
2013-01-02 MX 14 16
美国20 19
>>>> df2.reset_index()
date国家xy
0 2013-01-01 MX 11 14
1 2013-01-01美国23 13
2 2013-01-02 MX 14 16
3 2013-01-02美国20 19


I have a pandas dataframe/csv of the form

date        Country   Type     Val
2013-01-01  USA        x        23
2013-01-01  USA        y        13
2013-01-01  MX         x        11
2013-01-01  MX         y        14  
2013-01-02  USA        x        20
2013-01-02  USA        y        19
2013-01-02  MX         x        14
2013-01-02  MX         y        16

I want to convert this to a form

date       Country     x   y 
2013-01-01  USA        23  13
2013-01-01  MX         11  14
2013-01-02  USA        20  19
2013-01-02  MX         14  16

In general I am looking for a way to transform a table using unique values of a single column.

I have looked at pivot and groupby but didn't get the exact form.

HINT: possibly this is solvable by pivot but I haven't been able to get the form

解决方案

Probably not the most elegant way possible, but using unstack:

>>> df
         date Country Type  Val
0  2013-01-01     USA    x   23
1  2013-01-01     USA    y   13
2  2013-01-01      MX    x   11
3  2013-01-01      MX    y   14
4  2013-01-02     USA    x   20
5  2013-01-02     USA    y   19
6  2013-01-02      MX    x   14
7  2013-01-02      MX    y   16

>>> df.set_index(['date', 'Country', 'Type']).unstack('Type').reset_index()
            date Country  Val
Type                        x   y
0     2013-01-01      MX   11  14
1     2013-01-01     USA   23  13
2     2013-01-02      MX   14  16
3     2013-01-02     USA   20  19

A little more generally, and removing the strange hierarchical columns in the result:

>>> cols = [c for c in df.columns if c not in {'Type', 'Val'}]
>>> df2 = df.set_index(cols + ['Type']).unstack('Type')
>>> df2
                    Val
Type                  x   y
date       Country
2013-01-01 MX        11  14
           USA       23  13
2013-01-02 MX        14  16
           USA       20  19
>>> df2.columns = df2.columns.levels[1]
>>> df2.columns.name = None
>>> df2
                     x   y
date       Country
2013-01-01 MX       11  14
           USA      23  13
2013-01-02 MX       14  16
           USA      20  19
>>> df2.reset_index()
         date Country   x   y
0  2013-01-01      MX  11  14
1  2013-01-01     USA  23  13
2  2013-01-02      MX  14  16
3  2013-01-02     USA  20  19

这篇关于Pandas DataFrame:使用列的唯一值转换帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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