Python 重置系列索引而不转换为数据帧
当我使用Python 重置系列索引而不转换为数据帧,python,pandas,simulation,montecarlo,Python,Pandas,Simulation,Montecarlo,当我使用Series.reset_index()时,我的变量会变成DataFrame对象。有没有办法重置序列的索引而不产生此结果 上下文是基于概率的随机选择模拟(monte carlo sim),其中从序列中进行的选择被series.pop(item)忽略 我需要重新设置索引,因为我通过迭代创建累积频率序列。您可以在中尝试drop=True。重新设置索引,即序列。重新设置索引(drop=True,inplace=True) 根据: drop:boolean,默认为False 不要尝试将索引插入数
Series.reset_index()
时,我的变量会变成DataFrame
对象。有没有办法重置序列的索引而不产生此结果
上下文是基于概率的随机选择模拟(monte carlo sim),其中从序列中进行的选择被series.pop(item)
忽略
我需要重新设置索引,因为我通过迭代创建累积频率序列。您可以在
中尝试drop=True
。重新设置索引,即序列。重新设置索引(drop=True,inplace=True)
根据:
drop:boolean,默认为False
不要尝试将索引插入数据帧列
例如:
series = pd.Series([1,2,3,4,5,1,1])
print(series)
系列结果:
0 1
1 2
2 3
3 4
4 5
5 1
6 1
dtype: int64
从系列中选择一些值:
filtered = series[series.values==1]
print(filtered)
结果:
0 1
5 1
6 1
dtype: int64
0 1
1 1
2 1
dtype: int64
重置索引:
filtered.reset_index(drop=True, inplace=True)
print(filtered)
结果:
0 1
5 1
6 1
dtype: int64
0 1
1 1
2 1
dtype: int64
使用type(filtered)
仍返回Series
但是这似乎会清空序列,如果将重置索引Series
重新分配给自身,即f=f.reset\u index(drop=True,inplace=True)
您不需要在inplace
之后重新分配,它不会返回任何内容,它已经改变了原来的系列f
。如果确实需要分配给其他对象,请删除inplace=True
。