重置系列索引而不转换为 DataFrame

Mar*_*uel 4 python simulation montecarlo pandas

当我使用Series.reset_index()我的变量时,它变成了一个DataFrame对象。有没有办法重置系列的索引而不会导致此结果?

上下文是基于概率的随机选择模拟(蒙特卡罗模拟),其中从系列中进行的选择被省略series.pop(item)

我需要重置索引,因为我会进行迭代以创建累积频率系列。

stu*_*ent 6

您可以尝试drop=True.reset_indexseries.reset_index(drop=True, inplace=True)

根据文件

drop : 布尔值,默认为 False

不要尝试将索引插入到数据框列中。

例子:

series = pd.Series([1,2,3,4,5,1,1])
print(series)
Run Code Online (Sandbox Code Playgroud)

系列结果:

0    1
1    2
2    3
3    4
4    5
5    1
6    1
dtype: int64
Run Code Online (Sandbox Code Playgroud)

从系列中选择一些值:

filtered = series[series.values==1]
print(filtered)
Run Code Online (Sandbox Code Playgroud)

结果:

0    1
5    1
6    1
dtype: int64
Run Code Online (Sandbox Code Playgroud)

重置索引:

filtered.reset_index(drop=True, inplace=True)
print(filtered)
Run Code Online (Sandbox Code Playgroud)

结果:

0    1
1    1
2    1
dtype: int64
Run Code Online (Sandbox Code Playgroud)

type(filtered)仍然返回Series