Luca Lazzati
Luca Lazzati

Reputation: 7

Create a list of tuples out of a list in a Pandas dataframe

I have a dataframe like this:

Path                   |  Routes_tuples
_______________________________________

[OAK, PHX, FLL, PBG]   | 0
[OAK, SEA, FLL, PBG]   | 0
[OAK, LAS, FLL, PBG]   | 0
[OAK, DFW, FLL, PBG]   | 0
.............          | ...............

I want something like this:

Path                   |  Routes_tuples
_______________________________________

[OAK, PHX, FLL, PBG]   | [(OAK, PHX), (PHX, FLL),(FLL, PBG)]
[OAK, SEA, FLL, PBG]   | [(OAK, SEA), (SEA, FLL),(FLL, PBG)]
[OAK, LAS, FLL, PBG]   | [(OAK, LAS), (LAS, FLL),(FLL, PBG)]
[OAK, DFW, FLL, PBG]   | [(OAK, DFW), (DFW, FLL),(FLL, PBG)]
.............          | ...............

How can I do it with Python?

Upvotes: 0

Views: 51

Answers (1)

Buckeye14Guy
Buckeye14Guy

Reputation: 851

To obtain what you want, you just need to shift your lists and pair them up progressively.

import pandas as pd
series = pd.Series([['OAK', 'PHX', 'FLL', 'PBG'], ['OAK', 'SEA', 'FLL', 'PBG']])
couples = lambda ls: [(x,y) for x,y in zip(ls[:-1], ls[1:])]
series.apply(couples)

Output:

0    [(OAK, PHX), (PHX, FLL), (FLL, PBG)]
1    [(OAK, SEA), (SEA, FLL), (FLL, PBG)]

So in your case you might just be able to do df['Path'].apply(couples)

Upvotes: 2

Related Questions