楼主: sunrun19840306
1125 0

[作业] Groupby-Pandas-User Guide原文翻译5 [推广有奖]

  • 3关注
  • 1粉丝

已卖:362份资源

本科生

90%

还不是VIP/贵宾

-

威望
0
论坛币
1827 个
通用积分
5.4941
学术水平
2 点
热心指数
1 点
信用等级
0 点
经验
2868 点
帖子
62
精华
0
在线时间
97 小时
注册时间
2008-12-20
最后登录
2024-3-31

楼主
sunrun19840306 发表于 2020-5-16 13:30:26 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

DataFrame column selectionin GroupBy¶

Once you have created the GroupBy object from a DataFrame, youmight want to do something different for each of the columns. Thus, using [] similar to getting a column froma DataFrame, you can do:


一旦你已经用一个DF创造了一个GroupBy对象,你可能想要对每列做不 同的事情,因此使用中括号[]来得到DF中的一列,你可以做


In [53]: grouped =df.groupby(['A'])


用A分组后,得到两列,分别C和D


注意这里的grouped是DataFrameGroupBy


In [54]: grouped_C =grouped['C']


所以说和DF中获取一列是一样的。


In [55]: grouped_D= grouped['D']


但grouped_C和D是SeriesGroupBy



This is mainly syntactic sugar for the alternative and much moreverbose:


这一模一样的效果也可以用以下的语句得到


In [56]: df['C'].groupby(df['A'])


这个也是SeriesGroupBy


Additionally this method avoids recomputing the internalgrouping information derived from the passed key


另外,这个方法避免了重新计算这个内部的分级信息来源于使用过的关键词。



Iterating through groups¶

With the GroupBy object in hand, iterating through the groupeddata is very natural and functions similarly to itertools.groupby():


通过组进行迭代


在唾手可得GroupBy对象,可以对DataFrameGroupBy数据进行迭代是非常自然的,与下面这个函数相似。Itertools.groupby()


grouped = df.groupby('A')



In [58]: for name, group in grouped:


  ....:     print(name)


  ....:     print(group)


print(name)##就只是按照A分组的后分级标志形成列的名字,也就是A分组后的两个特征值bar二是foo


print(group)##可以看到是按照A分组展示的,A分组就只两个特征值一是bar二是foo,分别展示了BCD列


In thecase of grouping by multiple keys, the group name will be a tuple:


在有多个关键词的例子中,组的名字就是一个元组。Will be是事实表意


In [59]: for name, group in df.groupby(['A', 'B']):


  ....:     print(name)


  ....:     print(group)


名字分别是('bar','one')、('bar', 'three')、('bar', 'two')、('foo', 'one')、('foo', 'three')、('foo', 'two'),这只是tuple的key不是value


('bar','one')key


A B C D1 value


bar one2 -2


See Iteratingthrough groups还可以去查看用组迭代。



Selecting a group¶

A single group can be selected using get_group():


选择组,一个单个组可以通过get_group()来选择。


In [60]: grouped.get_group('bar')


Out[60]:


    A      B         C         D


1 bar    one  0.254161 1.511763


3 bar  three  0.215897 -0.990582


5  bar    two -0.077118  1.211526


按bar分组,得到这个对象是一个DataFrame


Or for an object grouped on multiple columns:


或者对于多个列对一个对象分组。


df.groupby(['A', 'B']).get_group(('bar', 'one'))


先对AB分组然后对bar one单独构建DF


Out[61]:


    A    B         C         D


1 bar  one  0.254161 1.511763


按bar分组,得到这个对象是一个DataFrame



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:pandas panda Group Guide guid

已有 1 人评分经验 学术水平 收起 理由
cheetahfly + 100 + 1 精彩帖子

总评分: 经验 + 100  学术水平 + 1   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-24 19:13