title: Pandas DataFrame - Combining one column's values with same index into list
body: <p>I've been at this issue for awhile to no avail. This is almost a duplicate of <a href="https://stackoverflow.com/questions/17438906/combining-rows-in-pandas">at least one other question on here</a>, but I can't quite figure out how to do exactly what I'm looking for from related answers online.</p>
  
  <p>I have a Pandas DataFrame (we'll call it <code>df</code>) that looks something like:</p>
  
  <pre><code>Name    Value        Value2
  'A'     '8.8.8.8'    'x'
  'B'     '6.6.6.6'    'y'
  'A'     '6.6.6.6'    'x'
  'A'     '8.8.8.8'    'x'
  </code></pre>
  
  <p>Where <code>Name</code> is the index. I want to convert this to something like that looks like:</p>
  
  <pre><code>Name    Value                     Value2
  'A'     ['8.8.8.8', '6.6.6.6']    'x'
  'B'     ['6.6.6.6']               'y'
  </code></pre>
  
  <p>So, basically, every <code>Value</code> that corresponds to the same index should be combined into a list (or a set, or a tuple) and that list made to be the <code>Value</code> for the corresponding index. And, as shown, <code>Value2</code> is the same between like-indexed rows, so it should just stay the same in the end.</p>
  
  <p>All I've done (successfully) is figure out how to make each element in the <code>Value</code> column into a list with:</p>
  
  <pre><code>df['Value'] = pd.Series([[val] for val in df['Value']])
  </code></pre>
  
  <p>In the question I linked at the start of this post, the recommended way to combine columns with duplicate indices offers a solution using <code>df.groupby(df.index).sum()</code>. I know I need something besides <code>df.index</code> as an argument to <code>groupby</code> since the <code>Value</code> column is treated as special, and I'm not sure what to put in place of <code>sum()</code> since that's not quite what I'm looking for.</p>
  
  <p>Hopefully it's clear what I'm looking for, let me know if there's anything I can elaborate on. I've also tried simply looping through the DataFrame myself, finding rows with the same index, combining the <code>Values</code> into a list and updating <code>df</code> accordingly. After trying to get this method to work for a bit I thought I'd look for a more Pandas-esque way of handling this problem.</p>
  
  <hr>
  
  <p>Edit: As a follow up to dermen's answer, that solution kind of worked. The <code>Values</code> did seem to concatenate correctly into a list. One thing I realized was that the <code>unique</code> function returns a <code>Series</code>, as opposed to a <code>DataFrame</code>. Also, I do have more columns in the actual setup than just <code>Name</code>, <code>Value</code>, and <code>Value2</code>. But I think I was able to get around both of the issues successfully with the following:</p>
  
  <pre><code>gb = df.groupby(tuple(df.columns.difference(['Value'])))
  result = pd.DataFrame(gb['Value'].unique(), columns=df.columns)
  </code></pre>
  
  <p>Where the first line gives an argument to <code>groupby</code> of the list of columns minus the <code>Value</code> column, and the second line converts the <code>Series</code> returned by <code>unique</code> into a <code>DataFrame</code> with the same columns as <code>df</code>.</p>
  
  <p>But I think with all of that in place (unless anyone sees an issue with this), almost everything works as intended. There does seem to be something that's a bit off here, though. When I try to output this to a file with <code>to_csv</code>, there are duplicate headers across the top (but only certain headers are duplicated, and there's no real pattern as to which as far as I can tell). Also, the <code>Value</code> lists are truncated, which is probably a simpler issue to fix. The <code>csv</code> output currenlty looks like:</p>
  
  <pre><code>Name    Value                   Value2    Name    Value2
  'A'     ['8.8.8.8' '7.7.7.7'    'x'                     
  'B'     ['6.6.6.6']             'y'
  </code></pre>
  
  <p>The above looks weird, but that is exactly how it looks in the output. Note that, contrary to the example presented at the start of this post, there are assumed to be more than 2 <code>Values</code> for <code>A</code> (so that I can illustrate this point). When I do this with the actual data, the <code>Value</code> lists get cut off after the first 4 elements.</p>