pandas.Index.to_series#

final Index.to_series(index=None, name=None)[源代码]#

创建一个 Series,其中索引和值都等于索引键。

与 map 结合用于返回基于索引的索引器非常有用。

Parameters:
indexIndex, optional

结果 Series 的 Index。如果为 None,则默认为原始索引。

namebool, default False

结果 Series 的 Name。如果为 None,则默认为原始索引的 name。

Returns:
Series

dtype 将基于 Index 值的类型。

参见

Index.to_frame

将 Index 转换为 DataFrame。

Series.to_frame

将 Series 转换为 DataFrame。

Examples

>>> idx = pd.Index(['Ant', 'Bear', 'Cow'], name='animal')

默认情况下,会重用原始索引和原始 name。

>>> idx.to_series()
animal
Ant      Ant
Bear    Bear
Cow      Cow
Name: animal, dtype: object

要强制创建新索引,请在 index 中指定新标签:

>>> idx.to_series(index=[0, 1, 2])
0     Ant
1    Bear
2     Cow
Name: animal, dtype: object

要覆盖结果列的 name,请指定 name

>>> idx.to_series(name='zoo')
animal
Ant      Ant
Bear    Bear
Cow      Cow
Name: zoo, dtype: object