Given the following data frame:
import pandas as pd
df=pd.DataFrame({'A':['a','b','c'],
'B':[[[1,2],[3,4],[5,6]],[[1,2],[3,4],[5,6]],[[1,2],[3,4],[5,6]]]})
df
A B
0 a [[1, 2], [3, 4], [5, 6]]
1 b [[1, 2], [3, 4], [5, 6]]
2 c [[1, 2], [3, 4], [5, 6]]
I'd like to create a new column ('C') containing the first value in each element of the tuple of column B like this:
A B C
0 a [[1, 2], [3, 4], [5, 6]] [1,3,5]
1 b [[1, 2], [3, 4], [5, 6]] [1,3,5]
2 c [[1, 2], [3, 4], [5, 6]] [1,3,5]
So far, I've tried:
df['C']=df['B'][0]
...but that only returns the first tuple ([1, 2]).
Thanks in advance!
df['B'].apply(lambda x: [e[0] for e in x])
work? OK just re-read it, see updated comment – Eliason