Khris - 5 months ago 18

Python Question

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

Example:

`import pandas as pd`

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

{'A':2,'B':'Test2','C':(77,u'orz')},\

{'A':3,'B':'Test3','C':(u'ASDFG',[1,2,3])}]

F = pd.DataFrame.from_dict(D)

print F

This is the normal output:

`A B C`

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

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]]
```