本文介绍了查找元素更改值 pandas 数据框的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
参考此问题/答案,有没有办法在不将其转换为numpy数组的情况下,对pandas数据框结构实现相同的功能?
Regaring to this question/answer, is there a way to accomplish the same function for a pandas dataframe structure without casting it as a numpy array?
推荐答案
s = pd.Series([1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 3, 4, 3, 4, 3, 4, 5, 5, 5])
print(s.diff()[s.diff() != 0].index.values)
OR:
df = pd.DataFrame([1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 3, 4, 3, 4, 3, 4, 5, 5, 5])
print(df[0].diff()[df[0].diff() != 0].index.values)
输出:
[0 5 8 9 10 11 12 13 14 15 16]
[ 0 5 8 9 10 11 12 13 14 15 16]
s.diff()[lambda x: x != 0].index.tolist()
输出:
[0, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16]
这篇关于查找元素更改值 pandas 数据框的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!