将Pandas GroupBy对象转换为DataFrame [英] Converting a Pandas GroupBy object to DataFrame

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

问题描述

  df1 = pandas.DataFrame({
Name :[爱丽丝,鲍勃,马洛里,马洛里,鲍勃,马洛里],
城市:[西雅图,西雅图,波特兰 ,Seattle,Portland]})

p>

 城市名称
0西雅图爱丽丝
1西雅图鲍勃
2波特兰马洛里
3西雅图马洛里
4西雅图鲍勃
5波特兰马洛里

分组非常简单:

  g1 = df1.groupby([Name,City]).count()
code>

并打印时产生一个 GroupBy 对象:

 城市名称
名称城市
爱丽丝西雅图1 1
鲍勃西雅图2 2
马洛里波特兰2 2
Seattle 1 1

但我最终想要的是另一个DataFrame对象,其中包含GroupBy对象中的所有行。换句话说,我想得到以下结果:

 城市名称
名称城市
Alice Seattle 1 1
Bob Seattle 2 2
Mallory Portland 2 2
Mallory Seattle 1 1

我不太清楚如何在熊猫文档中实现这一点。任何提示都会受到欢迎。

一个DataFrame。不过,它有一个分层索引:



pre $ 在[19]中:type(g1)
Out [19]: pandas.core.frame.DataFrame
$ b $ [20]:g1.index
Out [20]:
MultiIndex([('Alice','Seattle'),( ''Bob','Seattle'),('Mallory','Portland'),
('Mallory','Seattle')],dtype = object)

也许你想要这样的东西?

  In [ 21]:g1.add_suffix('_ Count')。reset_index()
Out [21]:
名称City City_Count Name_Count
0 Alice Seattle 1 1
1 Bob Seattle 2 2
2马洛里波特兰2 2
3马洛里西雅图1 1

 在[36]中:DataFrame({'count':df1.groupby([Name,City]] ).size()})。reset_index()
输出[36]:
名称城市数量
0 Alice Seattle 1
1 Bob Seattle 2
2 Mallory Portland 2
3 Mallory Seattle 1


I'm starting with input data like this

df1 = pandas.DataFrame( { 
    "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , 
    "City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"] } )

Which when printed appears as this:

   City     Name
0   Seattle    Alice
1   Seattle      Bob
2  Portland  Mallory
3   Seattle  Mallory
4   Seattle      Bob
5  Portland  Mallory

Grouping is simple enough:

g1 = df1.groupby( [ "Name", "City"] ).count()

and printing yields a GroupBy object:

                  City  Name
Name    City
Alice   Seattle      1     1
Bob     Seattle      2     2
Mallory Portland     2     2
        Seattle      1     1

But what I want eventually is another DataFrame object that contains all the rows in the GroupBy object. In other words I want to get the following result:

                  City  Name
Name    City
Alice   Seattle      1     1
Bob     Seattle      2     2
Mallory Portland     2     2
Mallory Seattle      1     1

I can't quite see how to accomplish this in the pandas documentation. Any hints would be welcome.

解决方案

g1 here is a DataFrame. It has a hierarchical index, though:

In [19]: type(g1)
Out[19]: pandas.core.frame.DataFrame

In [20]: g1.index
Out[20]: 
MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'),
       ('Mallory', 'Seattle')], dtype=object)

Perhaps you want something like this?

In [21]: g1.add_suffix('_Count').reset_index()
Out[21]: 
      Name      City  City_Count  Name_Count
0    Alice   Seattle           1           1
1      Bob   Seattle           2           2
2  Mallory  Portland           2           2
3  Mallory   Seattle           1           1

Or something like:

In [36]: DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
Out[36]: 
      Name      City  count
0    Alice   Seattle      1
1      Bob   Seattle      2
2  Mallory  Portland      2
3  Mallory   Seattle      1

这篇关于将Pandas GroupBy对象转换为DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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