如何汇总与数据框中特定ID相关的所有值?

时间:2018-04-18 16:47:12

标签: python pandas dataframe

假设我在数据框中有这样的列:

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;


namespace My.Crm.RetrieveMultipleConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType = Office365; Url = xxxx/;Username=xxxx;Password=xxxx";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                string fetchquery = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='account'>
    <attribute name='name' />
    <order attribute='name' descending='false' />
    <filter type='and'>
      <condition attribute='name' operator='like' value='%Tom%' />
    </filter>
  </entity>
</fetch>";

                var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
                {
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = false,
                        ReturnResponses = true
                    },
                    Requests = new OrganizationRequestCollection()
                };
                EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchquery));
                foreach (var c in accounts.Entities)
                {

                    Console.WriteLine("accountid: {0}", c.Attributes["name"]);                
                    Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
                    multipleRequest.Requests.Add(updateRequest);
                    c.Attributes["name"] = "New Name";
                    Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);

                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}

我有成千上万的这样的ID。我想用相同的id对所有值求和,以便得到如下数据框:

id        value
 a           2 
 a           1  
 b           8
 a           0  
 a           2

我如何在熊猫中这样做?

1 个答案:

答案 0 :(得分:1)

您应该使用.groupby

df.groupby('id').sum()