pandas 堆/ groupby创建一个新的数据框 [英] Pandas stack/groupby to make a new dataframe

查看:121
本文介绍了 pandas 堆/ groupby创建一个新的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建和重新排列数据集的问题。我看着大熊猫groupby功能,并认为它可能会帮助我做,但我没有经验,使它发生。
我创建了一个我的问题的例子:
我的df:

I have a problem of creating and rearrange a dataset. I Looked at the pandas groupby function and think it might help me do it but I am to inexperienced to make it happen. I have created an example of my problem below: my df:

     vehicle  color  a  b  c  d  A1  A2  A3  B1  B2  B3  C1  C2  C3  D1  D2  D3
resp                                                                           
1       bike  green  5  4  1  3   3   4   5   3   5   3 NaN NaN NaN NaN NaN NaN
2       walk    red  5  3  3  3   4   5   3   3   5   4 NaN NaN NaN NaN NaN NaN
3        car  green  4  2  3  3   4   3   5   4   5   5 NaN NaN NaN NaN NaN NaN
4        car   blue  4  5  4  4 NaN NaN NaN NaN NaN NaN   5   5   5   3   3   4
5        bus  black  2  4  4  3 NaN NaN NaN   2   3   3   2   2   1 NaN NaN NaN
6        car    red  4  2  3  3   3   4   4 NaN NaN NaN   4   4   4 NaN NaN NaN
7        bus   blue  5  5  2  3   3   3   5   4   3   2 NaN NaN NaN NaN NaN NaN
8       walk    red  3  3  4  3 NaN NaN NaN   5   5   5   5   3   3 NaN NaN NaN
9        car   blue  5  3  4  3   3   3   3 NaN NaN NaN   4   3   4 NaN NaN NaN

数据集包含受访者和问题答案。我想要做的是创建一个新的数据帧作为索引,以及受访者的回答如何重新排列的数据。
列a,b,c,d,车辆和颜色中的数据在新的数据框架中被堆叠起来(希望是正确的方式表达)。列A到C的值也在BL_val列下的新框中。只填写从大写字母(A1-D3)到小写字母(a,b,c,d)的数据,其余的是NAN。

The dataset contains respondents and answers to a questionare. What I would like to do is to make a new dataframe with resp as index and the data from how the respondents answered rearranged. The data in columns a,b,c,d, vehicle and color are stacked for the respondents (Hope thats the right way to express it) in the new dataframe. Also the values from columns A to C are in the new frame under columns BL_val. Only the data that corresponds from Capital letter (A1-D3) to small letter (a,b,c,d) are filled in. The rest are NAN.

I想从这里创建一个新的数据格式,它看起来像:

I would like to create a new datafram from this and it shoul look like:

ds:

     vehicle  color sl  sl_val  BL_val1  BL_val2  BL_val3
resp                                                     
1       bike  green  a       5        3        4        5
1       bike  green  b       4        3        5        3
1       bike  green  c       1      NaN      NaN      NaN
1       bike  green  d       3      NaN      NaN      NaN
2       walk    red  a       5        4        5        3
2       walk    red  b       3        3        5        4
2       walk    red  c       3      NaN      NaN      NaN
2       walk    red  d       3      NaN      NaN      NaN
3        car  green  a       4        4        3        5
3        car  green  b       2        4        5        5
3        car  green  c       3      NaN      NaN      NaN
3        car  green  d       3      NaN      NaN      NaN
4        car   blue  a       4      NaN      NaN      NaN
4        car   blue  b       5      NaN      NaN      NaN
4        car   blue  c       4        5        5        5
4        car   blue  d       4        3        3        4
5        bus  black  a       2      NaN      NaN      NaN
5        bus  black  b       4        2        3        3
5        bus  black  c       4        2        2        1
5        bus  black  d       3      NaN      NaN      NaN
6        car    red  a       4        3        4        4
6        car    red  b       2      NaN      NaN      NaN
6        car    red  c       3        4        4        4
6        car    red  d       3      NaN      NaN      NaN
7        bus   blue  a       5        3        3        5
7        bus   blue  b       5        4        3        2
7        bus   blue  c       2      NaN      NaN      NaN
7        bus   blue  d       3      NaN      NaN      NaN
8       walk    red  a       3      NaN      NaN      NaN
8       walk    red  b       3        5        5        5
8       walk    red  c       4        5        3        3
8       walk    red  d       3      NaN      NaN      NaN
9        car   blue  a       5        3        3        3
9        car   blue  b       3      NaN      NaN      NaN
9        car   blue  c       4        4        3        4
9        car   blue  d     NaN      NaN      NaN      NaN

我真的需要一些帮助,我不能弄清楚!

I really need some help with this, I cant figure it out!!

推荐答案

可能会有一个更可爱的方式来做这个,但是我发现使用 groupby 的模式使组,对它们执行显式操作,然后重组,通常是一种简单的方式得到我想要的当然,我可以花半个小时,提出一些比较优雅的东西,但是后来我根本就没有时间在这个时候出去玩..

There might be a more pandorable way to do this, but I find that the pattern of using groupby to make the groups, performing explicit operations on them, and then recombining, is often a simple way to get what I want. Sure, I could spend half an hour coming up with something more elegant, but then I wouldn't have any time to hang out on SO..

无论如何这样的东西?

df = df.set_index(["resp", "vehicle", "color"])

grouped = df.groupby(lambda x: x[0].lower(), axis=1)

new_grouped = []
for key, group in grouped:
    group.columns = ["sl_val"] + ["BL_val{}".format(i) for i in range(1,4)]
    group["sl"] = key
    new_grouped.append(group)

df2 = pd.concat(new_grouped).reset_index()

df2 = df2.sort(["resp", "vehicle", "color"]).set_index("resp")
df2 = df2[["vehicle", "color", "sl"] + [k for k in df2.columns if "_" in k]]







Starting from

>>> df = df.set_index(["resp", "vehicle", "color"])
>>> df
                    a  b  c  d  A1  A2  A3  B1  B2  B3  C1  C2  C3  D1  D2  D3
resp vehicle color                                                            
1    bike    green  5  4  1  3   3   4   5   3   5   3 NaN NaN NaN NaN NaN NaN
2    walk    red    5  3  3  3   4   5   3   3   5   4 NaN NaN NaN NaN NaN NaN
3    car     green  4  2  3  3   4   3   5   4   5   5 NaN NaN NaN NaN NaN NaN
4    car     blue   4  5  4  4 NaN NaN NaN NaN NaN NaN   5   5   5   3   3   4
5    bus     black  2  4  4  3 NaN NaN NaN   2   3   3   2   2   1 NaN NaN NaN
6    car     red    4  2  3  3   3   4   4 NaN NaN NaN   4   4   4 NaN NaN NaN
7    bus     blue   5  5  2  3   3   3   5   4   3   2 NaN NaN NaN NaN NaN NaN
8    walk    red    3  3  4  3 NaN NaN NaN   5   5   5   5   3   3 NaN NaN NaN
9    car     blue   5  3  4  3   3   3   3 NaN NaN NaN   4   3   4 NaN NaN NaN

我们可以按起始字母分组小写:

We can group by the starting letter in lowercase:

>>> grouped = df.groupby(lambda x: x[0].lower(), axis=1)

生成一组看起来像:

>>> next(iter(grouped))
('a',                     a  A1  A2  A3
resp vehicle color               
1    bike    green  5   3   4   5
2    walk    red    5   4   5   3
3    car     green  4   4   3   5
4    car     blue   4 NaN NaN NaN
5    bus     black  2 NaN NaN NaN
6    car     red    4   3   4   4
7    bus     blue   5   3   3   5
8    walk    red    3 NaN NaN NaN
9    car     blue   5   3   3   3)

然后我们只需更改名称,添加sl列,并使用 pd.concat 。最后一个比特简单地符合您所需的顺序。

Then we simply change the names, add the "sl" column, and recombine them using pd.concat. The last bits simply match your desired order.

最终结果:

>>> df2
     vehicle  color sl  sl_val  BL_val1  BL_val2  BL_val3
resp                                                     
1       bike  green  a       5        3        4        5
1       bike  green  b       4        3        5        3
1       bike  green  c       1      NaN      NaN      NaN
1       bike  green  d       3      NaN      NaN      NaN
2       walk    red  a       5        4        5        3
2       walk    red  b       3        3        5        4
2       walk    red  c       3      NaN      NaN      NaN
2       walk    red  d       3      NaN      NaN      NaN
3        car  green  a       4        4        3        5
3        car  green  b       2        4        5        5
3        car  green  c       3      NaN      NaN      NaN
3        car  green  d       3      NaN      NaN      NaN
4        car   blue  a       4      NaN      NaN      NaN
4        car   blue  b       5      NaN      NaN      NaN
4        car   blue  c       4        5        5        5
4        car   blue  d       4        3        3        4
5        bus  black  a       2      NaN      NaN      NaN
5        bus  black  b       4        2        3        3
5        bus  black  c       4        2        2        1
5        bus  black  d       3      NaN      NaN      NaN
6        car    red  a       4        3        4        4
6        car    red  b       2      NaN      NaN      NaN
6        car    red  c       3        4        4        4
6        car    red  d       3      NaN      NaN      NaN
7        bus   blue  a       5        3        3        5
7        bus   blue  b       5        4        3        2
7        bus   blue  c       2      NaN      NaN      NaN
7        bus   blue  d       3      NaN      NaN      NaN
8       walk    red  a       3      NaN      NaN      NaN
8       walk    red  b       3        5        5        5
8       walk    red  c       4        5        3        3
8       walk    red  d       3      NaN      NaN      NaN
9        car   blue  a       5        3        3        3
9        car   blue  b       3      NaN      NaN      NaN
9        car   blue  c       4        4        3        4
9        car   blue  d       3      NaN      NaN      NaN

这篇关于 pandas 堆/ groupby创建一个新的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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