如何在多列的groupby中获取第一个组?

时间:2018-04-12 14:53:37

标签: pandas

在我申请groupby之后,我一直想弄清楚如何才能返回第一组。

我的代码如下所示:

gb = df.groupby(['col1', 'col2', 'col3', 'col4'])['col5'].sum()

我想要的是第一个第一组输出。我一直在尝试使用get_group方法,但它一直在失败(也许是因为我按多列分组?)

以下是我的输出示例:

col1  col2  col3   col4  'sum'
 1     34   green   10    0.0
            yellow  30    1.5 
            orange  20    1.1 
 2     89   green   10    3.0 
            yellow   5    0.0 
            orange  10    1.0

我想要归还的是:

col1  col2  col3   col4  'sum'
 1     34   green   10    0.0
            yellow  30    1.5 
            orange  20    1.1 

(注意我在这里添加的'sum'列,以明确最后一列是什么,但pandas实际上没有命名该列)

4 个答案:

答案 0 :(得分:5)

您可以将get_groupgroups

一起使用
g=df.groupby(['col1','col2'])

g.get_group((list(g.groups)[0])).groupby(['col3','col4'])['col5'].sum()

答案 1 :(得分:4)

我相信你需要:

     public void updateData()
    {
        Console.WriteLine("Find Student");
        Console.Write("Student id: ");
        string code = Console.ReadLine();

        Studentdetails target = Data.getStudent(code);

        if (target == null)
        {
            Console.WriteLine("Record not found!");
        }
        else
        {
            Console.WriteLine("");
            Console.WriteLine("Name: " + target.name);
            Console.WriteLine("Student status: " + target.status);
            Console.WriteLine("Gender: " + target.gender.ToString());
            Console.WriteLine("Standard: " + target.standard);
            Console.WriteLine("1.Classroom: " + target.classroom);
            Console.WriteLine("2.Reg payment status:" + target.studReg);
            Console.WriteLine("3.Annual payment status:" + target.annual);
            Console.WriteLine("4.Satu Keluarga Payment status:" +target.sk);
            Console.WriteLine("5.Monthly payments due: " + target.payment);

            Console.Write("Select details to update (numbers given): ");
            int choice = Convert.ToInt16(Console.ReadLine());
            if (choice == 1)
            {
                Console.Write("\nNew classroom: ");
                target.classroom = Console.ReadLine();

            }

            if (choice == 2)
            {
                target.studReg = "Paid";

            }

            if (choice == 3)
            {
                target.annual = "Paid";


            }

            if (choice == 4)
            {
                target.sk = "Paid";
            }

            if (choice == 5)
            {
                int paid;
                Console.Write("\nTotal months just paid?: ");
                paid = Convert.ToInt16(Console.ReadLine());

                if (paid > target.payment)
                {
                    Console.WriteLine("Invalid amount of payment");
                }

                else
                {

                    int lastpayment = target.payment;
                    int newpayment = lastpayment - paid;
                    target.payment = newpayment;


                }
            }

            student_list[target.id] = target;


        }

DataFrame.xs

idx = df.index.get_level_values(0)
df = df[idx == idx[0]] 

答案 2 :(得分:4)

gb = df.groupby(['col1', 'col2', 'col3', 'col4'])['col5'].sum()

gb.loc[[gb.index.levels[0][0]]])

答案 3 :(得分:0)

for group_id, group_df in df.groupby(['col1', 'col2', 'col3', 'col4']):
    break

迭代您的 groupby 对象并在第一次迭代后停止。变量 group_id 和 group_df 将包含您的第一个组。

一种丑陋的解决方法,但有效。