Khris Khris - 1 year ago 44
Python Question

How do I get all values from one position in a tuple in a pandas dataframe column?

I have a pandas dataframe where several columns are filled with tuples with two values each, mixed types.


import pandas as pd

D = [{'A':1,'B':'Test1','C':('C1',True)},\

F = pd.DataFrame.from_dict(D)

print F

This is the normal output:

0 1 Test1 (C1, True)
1 2 Test2 (77, orz)
2 3 Test3 (ASDFG, [1, 2, 3])

And what I want is e.g. the second value from each tuple in column C, that is an output like:

True, orz, [1,2,3]

In Numpy you can do this:

import numpy as np
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print A[:,0]

Giving you:

[1 4 7]

But this doesn't work in pandas, so is there any way to do this or do I have to transform the data in a different way?

Answer Source

One way is to use a list comprehension and extract the second item in the tuple pair.

>>> [tup[1] for tup in F.C]
[True, u'orz', [1, 2, 3]]