0

I have pandas dataframe like this (single column):

request_headers
0   {'name': 'name1', value: 'value1'}
0   {'name': 'name2', value: 'value2'}
.....

Would like to aggregate many such records to a single record with a column which is a list of the above directories, like this:

request_headers
0   [{'name': 'name1', value: 'value1'}, {'name': 'name2', value: 'value2'}]

How to do that ? (tried with agg({col: lambda x: ",".join(x)}) and few similar but failing)

Thanks,

Update: Answer somebody provided is not correct, it's easy to do it for non-dict values, but for dict values we got error that it's unhashable. So the question is still valid. The problem here is that column value is a dict.

Update2: OK, sorry, one more example:

details = {
    'request_headers' : [{"name": "name1", "value": "value1"}, {"name": "name2", "value": "value2"}],
}
  
df = pd.DataFrame(details)
df.groupby(df.index).agg(list)

is giving me:

0   [{'name': 'name1', 'value': 'value1'}]
1   [{'name': 'name2', 'value': 'value2'}]

While i would like to get:

0 [{'name': 'name1', 'value': 'value1'},{'name': 'name2', 'value': 'value2'}]

So technically it's not even groupby, but some concatenation ? (i have actually exploded my df before and need to "implode" it now)

user2913139
  • 557
  • 2
  • 5
  • 13
  • `df.groupby(df.index).agg(list)` – anky Jan 26 '23 at 17:13
  • both answers are not correct, with those i am getting error "Uhashable type: dict". With dict it's a bit more difficult then non-dict, so my question is still valid and not duplicate – user2913139 Jan 26 '23 at 19:06
  • Sure, can you please edit your question to show how this does not work. It works on my system. please provide code to reproduce your dataframe and the code you tried. Happy to reopen. – anky Jan 26 '23 at 19:12
  • OK, added Update2 section, could you please help ? Thanks ! – user2913139 Jan 27 '23 at 06:04
  • So you need: `df['request_headers'].tolist()` for a list and `pd.Series([df['request_headers'].tolist()])` for a series object ? the reason I had to use group by is because in your first example, you had both index as 0 and expected output was series :) It wasnt clear – anky Jan 27 '23 at 06:17

0 Answers0