Pandas 系列是带有轴标签的一维ndarray。标签不必是唯一的,但必须是可哈希的类型。该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作。
Pandas Series.reset_index()函数使用索引重置生成一个新的DataFrame或Series。当索引需要用作列时,这很方便。
用法: Series.reset_index(level=None, drop=False, name=None, inplace=False)
参数:
level:对于具有多索引的系列
drop:只需重置索引,而无需将其作为列插入新的DataFrame中。
name:用于包含原始Series值的列的名称。
inplace:在适当位置修改系列
返回:结果:系列
范例1:采用Series.reset_index()函数重置给定Series对象的索引。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([10, 25, 3, 11, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
输出:
现在我们将使用Series.reset_index()函数重置给定系列对象的索引。
# reset the index
result = sr.reset_index()
# Print the result
print(result)
输出:
正如我们在输出中看到的,Series.reset_index()函数已将给定Series对象的索引重置为默认值。它保留了索引,并将其转换为列。
范例2:采用Series.reset_index()函数重置给定Series对象的索引。不要保留给定系列对象的原始索引标签。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([10, 25, 3, 11, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
输出:
现在我们将使用Series.reset_index()函数重置给定系列对象的索引,我们还将删除原始索引标签。
# reset the index
result = sr.reset_index(drop = True)
# Print the result
print(result)
输出:
正如我们在输出中看到的,Series.reset_index()函数已将给定Series对象的索引重置为默认值。它删除了原始索引。