如何在groupby上执行函数会导致python中的pandas?

时间:2013-01-28 17:02:27

标签: python csv pandas

我已使用此代码计算每个群集中每个用户的不同质量指标的值

>>> for name, group in df.groupby(["Cluster_id", "User"]):
...     print 'group name:', name
...     print 'group rows:'
...     print group
...     print 'counts of Quality values:'
...     print group["Quality"].value_counts()
...     raw_input()
...     

但现在我得到的输出为

group rows:
                tag                       user                    quality  cluster
676    black fabric  http://steve.nl/user_1002          usefulness-useful        1
708      blond wood  http://steve.nl/user_1002          usefulness-useful        1
709      blond wood  http://steve.nl/user_1002    problematic-misspelling        1
1410         eames?  http://steve.nl/user_1002      usefulness-not_useful        1
1411         eames?  http://steve.nl/user_1002  problematic-misperception        1
3649  rocking chair  http://steve.nl/user_1002          usefulness-useful        1
3650  rocking chair  http://steve.nl/user_1002  problematic-misperception        1
counts of Quality Values:
usefulness-useful            3
problematic-misperception    2
usefulness-not_useful        1
problematic-misspelling      1

我现在要做的是检查条件,即:

if quality==usefulness-useful:
 good = good + 1
else:
 bad = bad + 1

我尝试写输出:

counts of Quality Values:
usefulness-useful            3
problematic-misperception    2
usefulness-not_useful        1
problematic-misspelling      1

进入变量并尝试逐行遍历变量,但它不起作用。有人可以给我一些关于如何对某些行进行计算的建议。

1 个答案:

答案 0 :(得分:3)

一旦有了一个组,就可以使用.iterrows()方法逐行迭代。它为您提供行索引和行本身:

In [33]: for row_number, row in group.iterrows():
   ....:     print row_number
   ....:     print row
   ....:     
676
Tag                        black fabric
User          http://steve.nl/user_1002
Quality               usefulness-useful
Cluster_id                            1
Name: 676
708
Tag                          blond wood
User          http://steve.nl/user_1002
Quality               usefulness-useful
Cluster_id                            1
Name: 708
[etc]

并且每个行都可以像字典一样索引,例如:

In [48]: row
Out[48]: 
Tag                       rocking chair
User          http://steve.nl/user_1002
Quality       problematic-misperception
Cluster_id                            1
Name: 3650

In [49]: row["User"]
Out[49]: 'http://steve.nl/user_1002'

In [50]: row["Tag"]
Out[50]: 'rocking chair'

所以你可以编写你的循环

good = 0
bad = 0
for row_number, row in group.iterrows():
    if row['Quality'] == 'usefulness-useful':
        good += 1
    else:
        bad += 1
print 'good', good, 'bad', bad

给出了

good 3 bad 4

如果对你有意义的话,这是一个非常好的方法。另一种方法是直接使用Quality列中的计数:

In [54]: counts = group["Quality"].value_counts()

In [55]: counts
Out[55]: 
usefulness-useful            3
problematic-misperception    2
usefulness-not_useful        1
problematic-misspelling      1

In [56]: counts['usefulness-useful']
Out[56]: 3

因为坏=总计 - 好 - 我们有

In [57]: counts.sum() - counts['usefulness-useful']
Out[57]: 4