划分系列中的元组
Dividing tuples in a series
如何划分熊猫系列:
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
进入 pandas 数据框,例如:
Index Value
a ball
b cat
c plane
如果需要一列,请使用 str[1]
和 Series.to_frame
DataFrame
:
ser.str[1].to_frame('Value')
或者如果需要 2 列 DataFrame
:
ser.str[1].rename_axis('Index').reset_index(name='Value')
如何划分熊猫系列:
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
进入 pandas 数据框,例如:
Index Value
a ball
b cat
c plane
如果需要一列,请使用 str[1]
和 Series.to_frame
DataFrame
:
ser.str[1].to_frame('Value')
或者如果需要 2 列 DataFrame
:
ser.str[1].rename_axis('Index').reset_index(name='Value')