从Pandas

时间:2017-05-07 01:02:07

标签: python pandas

我是一个新的Python转换器(来自Matlab)。我正在使用pandas groupby函数,我被一个看似简单的问题绊倒了。我已经编写了一个自定义函数,我apply到分组的df,返回4个不同的值。其中三个值很有效,但另一个值给我一个错误。这是原始df

Index,SN,Date,City,State,ID,County,Age,A,B,C
0,32,9/1/16,X,AL,360,BB County,29.0,negative,positive,positive
1,32,9/1/16,X,AL,360,BB County,1.0,negative,negative,negative
2,32,9/1/16,X,AL,360,BB County,10.0,negative,negative,negative
3,32,9/1/16,X,AL,360,BB County,11.0,negative,negative,negative
4,35,9/1/16,X,AR,718,LL County,67.0,negative,negative,negative
5,38,9/1/16,X,AR,728-13,JJ County,3.0,negative,negative,negative
6,38,9/1/16,X,AR,728-13,JJ County,8.0,negative,negative,negative
7,30,9/1/16,X,AR,728-13,JJ County,8.0,negative,negative,negative
8,30,9/1/16,X,AR,728-13,JJ County,14.0,negative,negative,negative
9,30,9/1/16,X,AR,728-13,JJ County,5.0,negative,negative,negative
...

这是转换数据的功能。基本上,它计算的是“正面”的数量。值和组中的观察总数。我还希望它返回ID值,这就是问题所在:

def _ct_id_pos(grp):
    return grp['ID'][0], grp[grp.A == 'positive'].shape[0], grp[grp.B == 'positive'].shape[0], grp.shape[0]

apply _ct_id_pos函数对按DateSN分组的数据:

FullMx_prime = FullMx.groupby(['Date', 'SN']).apply(_ct_id_pos).reset_index()

因此,该方法应返回如下内容:

     Date  SN   ID       0
0  9/1/16  32  360  (360,2,1,4)
1  9/1/16  35  718  (718,0,0,1)
2  9/2/16  38  728  (728,1,0,2)
3  9/3/16  30  728  (728,2,0,3)

但是,我一直收到以下错误:

...
KeyError: 0

显然,它不喜欢这部分功能:grp['ID'][0]。我只想取grp['ID']的第一个值,因为 - 如果有多个值 - 它们应该都是相同的(即,我可以取最后一个,无关紧要)。我已尝试过其他方式进行索引,但无济于事。

1 个答案:

答案 0 :(得分:2)

public class ApplicationUser : IdentityUser { // some custom fields } public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public string AuthorId {get;set} [ForeignKey("AuthorId")] public virtual ApplicationUser Author {get;set;} } public class MyContext :IdentityDbContext<ApplicationUser> { public MyContext(): base("DefaultConnection", false){ } public DbSet<Comment> Comments { get; set; } //... other DbSets } 更改为grp['ID'][0]

您遇到的问题是grp.iloc[0]['ID']选择了一列并返回grp['ID']。哪个是直截了当的,你可以合理地期望pandas.Series会选择第一个元素。但[0]实际上是根据Series的索引进行选择的,在这种情况下,索引来自分组的数据帧。因此,[0]并不总是有效的索引。

<强>代码:

0

测试代码:

def _ct_id_pos(grp):
    id = grp.iloc[0]['ID']
    a = grp[grp.A == 'positive'].shape[0]
    b = grp[grp.B == 'positive'].shape[0]
    sz = grp.shape[0]

    return id, a, b, sz

<强>结果:

df = pd.read_csv(StringIO(u"""
    Index,SN,Date,City,State,ID,County,Age,A,B,C
    0,32,9/1/16,X,AL,360,BB County,29.0,negative,positive,positive
    1,32,9/1/16,X,AL,360,BB County,1.0,negative,negative,negative
    2,32,9/1/16,X,AL,360,BB County,10.0,negative,negative,negative
    3,32,9/1/16,X,AL,360,BB County,11.0,negative,negative,negative
    4,35,9/1/16,X,AR,718,LL County,67.0,negative,negative,negative
    5,38,9/1/16,X,AR,728-13,JJ County,3.0,negative,negative,negative
    6,38,9/1/16,X,AR,728-13,JJ County,8.0,negative,negative,negative
    7,30,9/1/16,X,AR,728-13,JJ County,8.0,negative,negative,negative
    8,30,9/1/16,X,AR,728-13,JJ County,14.0,negative,negative,negative
    9,30,9/1/16,X,AR,728-13,JJ County,5.0,negative,negative,negative
    """), header=0, index_col=0)

print(df.groupby(['Date', 'SN']).apply(_ct_id_pos).reset_index())