python - 从 Pandas 中的分组数据框中获取单个值

标签 python pandas

我是一名新的 Python 皈依者(来自 Matlab)。我正在使用 Pandas groupby函数,我被一个看似简单的问题绊倒了。我编写了一个自定义函数,我 apply到返回 4 个不同值的分组 df。其中三个值运行良好,但另一个值给了我一个错误。这是原文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函数对按 Date 分组的数据进行处理和SN :

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'] 的第一个值因为——如果有多个值——它们应该都是相同的(即,我可以采用最后一个,这并不重要)。我尝试过其他索引方法,但没有成功。

最佳答案

grp['ID'][0] 更改为 grp.iloc[0]['ID']

您遇到的问题是由于 grp['ID'] 选择一列并返回 pandas.Series 造成的。这很简单,您可以合理地预期 [0] 将选择第一个元素。但 [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())

结果:

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

关于python - 从 Pandas 中的分组数据框中获取单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43826839/

相关文章:

python - 截至当前季度的 Pandas Dataframe 前向填充

python - 当包含 NaN 时使用 "new information"更新 Pandas 数据帧

python - 确保至少安装了一个额外的 Python 包

python - numpy 和 pandas timedelta 错误

Python Pandas 将数据移动到新行

python - 'utf- 8' codec can' t 解码位置 11 中的字节 0x92 : invalid start byte

python - Requests/urllib 在 Flask/Apache/mod_wsgi/Windows 中不起作用

python - 使用 vim 打开新的 python 文件时自动 header

python - sklearn 随机森林可以直接处理分类特征吗?

numpy - 从整数创建 tz 感知 pandas 时间戳对象