使用熊猫对不同行数据进行分组

时间:2020-04-25 15:30:04

标签: python pandas dataframe

我的数据设置如下:

Class     Students

Seven       10

Seven       15

Five        12

Two         23

Two         36

Ten         16

Ten         10

Ten         5

我需要将它们按行分组,并进一步进行操作:

必需的输出:

Class   Total

Seven   10
        15

Five    12

Two     23
        36

Ten     16
        10
        5

我刚刚提供了一个样本数据,我的实际数据集非常大。

2 个答案:

答案 0 :(得分:2)

赞:

In [536]: df = df.groupby('Class').sum().reset_index() 

In [537]: df                                                                                                                                                         
Out[537]: 
   Class  Students
0   Five      12.0
1  Seven      25.0
2    Ten      31.0
3    Two      59.0

答案 1 :(得分:1)

尝试一下:

@model IgnitionHub2._0.Models.Car
@{
    ViewBag.Title = "Car Search Page";
}


<h2>Cars</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div class="center-div">
    <div class="form-inline">
        @Html.DropDownListFor(model => model.CarLotID, new SelectList(Model.CarLotList, "CarLotID", "LotName"), "Select Car Lot", new { @class = "form-control" })
        @Html.DropDownListFor(model => model.Model.MakeID, new SelectList(Model.MakeList, "MakeID", "Name"), "Select Make", new { @class = "form-control" })
        @Html.DropDownListFor(model => model.ModelID, new SelectList(Model.ModelList, "ModelID", "Name"), "Select Model", new { @id = "ModelID", @class = "form-control" })
        @Html.DropDownListFor(model => model.Available, new[] { new SelectListItem { Text = "Yes", Value = "true" }, new SelectListItem { Text = "No", Value = "false" } }, "Available", new { @id = "Available", @class = "form-control" })
        <button id="search">Search</button>
    </div>
</div>
<div id="searchResults">
    @{Html.RenderPartial("_Index", Model.CarList);}
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    <script type="text/javascript">
    $(document).ready(
        function () {
        var makeUrl = '@Url.Action("GetCarDetails")';
        var models = $('#ModelID')
        $('#Model_MakeID').change(function () {
            models.empty();
                $.getJSON(makeUrl, { MakeID: $(this).val() },function(data){
                    if (!data) {
                        return;
                    }
                    models.append($('<option></option>').val('').text('Please select'));
                    $.each(data, function(index, item) {
                        models.append($('<option value=' + item.ModelID + '></option>').text(item.Name));
                    });
                });
            })
        })
        $(document).ready(function () {
         var url = '@Url.Action("DisplaySearchResults","Car")';
        $('#search').click(function () {
            var carLotID = $('#CarLotID').val();
            var makeID = $('#Model_MakeID').val();
            var modelID = $('#ModelID').val();
            var available = $('#Available').val();
            $('#searchResults').load(url + "?CarLotID=" + carLotID + "&MakeID=" + makeID + "&ModelID=" + modelID +"&Available=" + available);
        })
        })
    </script>
}