scott martin
scott martin

Reputation: 1293

Pandas - Converting data frame into a specific format

I have a Dataframe that has two columns as below:

col_a,col_b
10,32
23,43
32,64

I am trying to get the Dataframe converted to the below format:

[(10,32),(23,43),(32,64)]

Upvotes: 0

Views: 41

Answers (1)

jezrael
jezrael

Reputation: 862711

Use list comprehension or map with convert lists to tuples:

L = [tuple(x) for x in df.values.tolist()]

L = list(map(tuple, df.values.tolist()))

Another solution with zip and transpose:

L = list(zip(*df.T.values.tolist()))

print (L)
[(10, 32), (23, 43), (32, 64)]

Upvotes: 1

Related Questions