简体   繁体   中英

Data wrangling with python pandas

I'm struggling to make do some data wrangling on a pandas dataframe. I've been stuck on this problem for 2 days now.

I've got a dataframe that looks like this:

['a','e']
['b','f']
['c','g']
['d','h']
['a','i']
['b','j']
['c','k']
['d','l']

And I need to turn it into looking like this

['a',['e','i']]
['b',['f','j']]
['c',['g','k']]
['d',['h','l']]

So basically pivoting the original dataframe around the first column and then creating a list of strings from the second column.

thanks

You could use groupy/agg :

import pandas as pd

data = [['a','e'], ['b','f'], ['c','g'], ['d','h'], ['a','i'], ['b','j'], 
        ['c','k'], ['d','l']]

df = pd.DataFrame(data, columns=['first', 'second'])
print(df.groupby(['first']).agg(lambda x: x.tolist()))

yields

       second
first        
a      [e, i]
b      [f, j]
c      [g, k]
d      [h, l]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM