将一系列元组转换为一系列每个元组的第 n 个元素

Transforming a series of tuples into a series of the nth element of each tuple

是否有一种干净的方法可以将 Series 个元组转换为另一个 Series,每个元组中包含第一个(或第 n 个)元素?例如,

ser = pd.Series([ (0,1,2), (3,4,5) ])

应该转换成由0和3组成的系列:

0  0
1  3

(其中 0,1 是索引)。谢谢

使用 .str 访问器:

In [90]: ser.str[0]
Out[90]:
0    0
1    3
dtype: int64

这是 AFAIK 向量化 Series.str.get(N) 方法的简写形式:

In [92]: ser.str.get(0)
Out[92]:
0    0
1    3
dtype: int64

您可以使用 Series.apply to apply an arbitrary function to your Series. The operator.itemgetter 将 return 一个函数来访问您想要的任何成员:

get0 = operator.itemgetter(0)
my0 = myseries.apply(get0)