首页 > 解决方案 > 在一个系列中划分元组

问题描述

如何划分熊猫系列:

d = {'a': ('a','ball'), 'b': ('b','cat'), 'c': ('c','plane')}
ser = pd.Series(data=d, index=['a', 'b', 'c'])
ser

a     (a, ball)
b      (b, cat)
c    (c, plane)
dtype: object

进入一个熊猫数据框,如:

Index Value
a     ball
b     cat
c     plane

标签: pandastuplesseries

解决方案


如果需要一列str[1],请使用:Series.to_frameDataFrame

ser.str[1].to_frame('Value')

或者如果需要 2 列DataFrame

ser.str[1].rename_axis('Index').reset_index(name='Value')

推荐阅读