【问题标题】:Dividing tuples in a series在一个系列中划分元组
【发布时间】:2021-09-13 08:11:02
【问题描述】:

熊猫系列如何划分:

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

【问题讨论】:

    标签: pandas tuples series


    【解决方案1】:

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

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

    或者如果需要2列DataFrame:

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

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2016-04-23
      相关资源
      最近更新 更多