title: Grouping a dict by a dynamic key name and aggregating some of the keys of a nested dict in Python
body: <p>I am struggling with grouping a nested dict by a key (in the nested dict) and by aggregating some of the nested dict's data.
  I hope that someone here can give me some helpful hints since I am not making any progress. I am using Python 3.6 and I looked at the collections and pandas module, figuring that the pandas module might contain the stuff I need to reach my goal.</p>
  
  <p>The following dict is provided:</p>
  
  <pre><code>
  {
    12345: {
      'date': '2019-07-26',
      'time_spent': 0.5,
      'color': 'yellow',
      'drive_id': 1804
    },
    54321: {
      'date': '2019-07-26',
      'time_spent': 1.5,
      'color': 'yellow',
      'drive_id': 3105
    },
    11561: {
      'date': '2019-07-25',
      'time_spent': 1.25,
      'color': 'red',
      'drive_id': 1449
    },
    12101: {
      'date': '2019-07-25',
      'time_spent': 0.25,
      'color': 'red',
      'drive_id': 2607
    },
    12337: {
      'date': '2019-07-24',
      'time_spent': 2.0,
      'color': 'yellow',
      'drive_id': 3105
    },
    54123: {
      'date': '2019-07-24',
      'time_spent': 1.5,
      'color': 'yellow',
      'drive_id': 4831
    },
    15931: {
      'date': '2019-07-19',
      'time_spent': 3.0,
      'color': 'yellow',
      'drive_id': 3105
    },
    13412: {
      'date': '2019-07-19',
      'time_spent': 1.5,
      'color': 'red',
      'drive_id': 1449
    }
  }
  
  </code></pre>
  
  <p>Consider it as a list of test drives a car seller had during these days, with time spent on a single test drive and having the sales opportunity evaluated by colors. Now, I need to group this data:</p>
  
  <ul>
  <li>Group it by date, so a new dict may contain the single dates as keys</li>
  <li>Aggregate the time_spent on a single date and provide a sum for this date</li>
  <li>Bring the colors with me, but if the colors mix up for one day (e.g. having red and yellow), red always wins</li>
  <li>For each date, have an aggregated list of drive_id, comma separated</li>
  <li>Throw away the key names of the top level dict </li>
  </ul>
  
  <p>So it might just look like this when I transform the data manually:</p>
  
  <pre><code>{
    '2019-07-26':
    {
      'time_spent': '2.0',
      'color': 'yellow',
      'drive_id': '1804, 3105',
  
    },
    '2019-07-25':
    {
      'time_spent': '1.5',
      'color': 'red',
      'drive_id': '1449, 2607',
  
    },
    '2019-07-24':
    {
      'time_spent': '3.5',
      'color': 'yellow',
      'drive_id': '3105, 4831',
  
    },
    '2019-07-19':
    {
      'time_spent': '4.5',
      'color': 'red',
      'drive_id': '1449, 3105',
    }
  
  }
  
  </code></pre>
  
  <p>Now where is my obstacle? Clearly, I have limited Python skills and I struggle with the dict key name (e.g. 13412) being generated dynamically. I found this solution here (<a href="https://stackoverflow.com/questions/38881679/group-pandas-dataframe-by-a-nested-dictionary-key/38881727#comment101189029_38881727">Group pandas dataframe by a nested dictionary key</a>) but I can't apply this solution to my case since the dict key name is not known here in advance. So I basically tried to create a pandas DataFrame and group the original dict by date first, but I already fail there.</p>
  
  <p>I apologize if I might have overlooked something in the pandas docs or a question here at StackOverflow. I would highly appreciate it if someone could give me a hint and explain to me how to approach such a case.</p>