如何循环分组的 Pandas 数据框?

2023-10-19Python开发问题
5

本文介绍了如何循环分组的 Pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

数据帧:

  c_os_family_ss c_os_major_is l_customer_id_i
0      Windows 7                         90418
1      Windows 7                         90418
2      Windows 7                         90418

代码:

print df
for name, group in df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)):
    print name
    print group

我正在尝试遍历聚合数据,但出现错误:

I'm trying to just loop over the aggregated data, but I get the error:

ValueError:解包的值太多

ValueError: too many values to unpack

@EdChum,这是预期的输出:

@EdChum, here's the expected output:

                                                    c_os_family_ss  
l_customer_id_i
131572           Windows 7,Windows 7,Windows 7,Windows 7,Window...
135467           Windows 7,Windows 7,Windows 7,Windows 7,Window...

                                                     c_os_major_is
l_customer_id_i
131572           ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...
135467           ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...

输出不是问题,我希望循环遍历每个组.

The output is not the problem, I wish to loop over every group.

推荐答案

df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)) 确实已经返回一个数据框,所以你不能再循环组了.

df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)) does already return a dataframe, so you cannot loop over the groups anymore.

一般:

  • df.groupby(...) 返回一个 GroupBy 对象(DataFrameGroupBy 或 SeriesGroupBy),通过这个,您可以遍历组(如文档此处中所述).您可以执行以下操作:

  • df.groupby(...) returns a GroupBy object (a DataFrameGroupBy or SeriesGroupBy), and with this, you can iterate through the groups (as explained in the docs here). You can do something like:

grouped = df.groupby('A')

for name, group in grouped:
    ...

  • 当您在 groupby 上应用函数时,在您的示例中 df.groupby(...).agg(...) (但这也可以是 transform, apply, mean, ...),你组合应用函数的结果将不同的组放在一个数据框中(groupby 的split-apply-combine"范式的应用和组合步骤).因此,其结果将始终是 DataFrame(或 Series,具体取决于应用的功能).

  • When you apply a function on the groupby, in your example df.groupby(...).agg(...) (but this can also be transform, apply, mean, ...), you combine the result of applying the function to the different groups together in one dataframe (the apply and combine step of the 'split-apply-combine' paradigm of groupby). So the result of this will always be again a DataFrame (or a Series depending on the applied function).

    这篇关于如何循环分组的 Pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

  • 相关推荐

    在xarray中按单个维度的多个坐标分组
    groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
    2024-08-22 Python开发问题
    15

    Pandas中的GROUP BY AND SUM不丢失列
    Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
    2024-08-22 Python开发问题
    17

    pandas 有从特定日期开始的按月分组的方式吗?
    Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
    2024-08-22 Python开发问题
    10

    GROUP BY+新列+基于条件的前一行抓取值
    Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
    2024-08-22 Python开发问题
    18

    PANDA中的Groupby算法和插值算法
    Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
    2024-08-22 Python开发问题
    11

    PANAS-基于列对行进行分组,并将NaN替换为非空值
    Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
    2024-08-22 Python开发问题
    10