funerr
funerr

Reputation: 8166

How should I work with Column of Type List in Pandas

So for example I would like to do something like this:

  name                transport  price
0  AAA  [cars, bikes, airplane]    200
1  BBB    [boat, cars, walking]    100
2  CCC         [airplane, cars]     50

Should I convert the a column to a different data structure so it will be easier to work with the data?


PS: I thought that I could use foreign keys (SQL) to work with the data, is this something pandas is capable of?

PS: I can't duplicate the rows as it will ruin the data (duplicating the price too).

Upvotes: 0

Views: 49

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61910

IIUC, you could use explode (pandas 0.25) + groupby:

result = df.explode('transport').groupby('transport')['price'].sum()
print(result)

Output

transport
airplane    250
bikes       200
boat        100
cars        350
walking     100
Name: price, dtype: int64

If you are not using pandas 0.25, use any of the answers here.

Upvotes: 2

Related Questions