- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有如下所示的这一系列整数
from pandas import Series
s = Series([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
我想看看数字在整个系列中变化了多少次,这样我就可以执行以下操作并获得预期的结果。
[i != s[:-1][idx] for idx, i in enumerate(s[1:])]
Out[5]:
[True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
False,
False,
False,
False]
从那里我可以简单地计算出 True 的当前数量。但这显然不是在 pandas 系列上操作的最佳方式,并且我在性能很重要的情况下添加了此方法,因此我执行了以下操作,期望得到相同的结果,但我感到非常惊讶和困惑。
s[1:].ne(s[:-1])
Out[4]:
0 True
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 False
15 False
16 False
17 False
18 False
19 False
20 False
21 False
22 False
23 False
24 False
25 False
26 False
27 False
28 False
29 False
30 False
31 False
32 False
33 False
34 False
35 False
36 False
37 False
38 False
39 True
dtype: bool
使用 Series.ne
方法的输出不仅对我来说没有任何逻辑意义,而且输出也比任何一个输入都长,这尤其令人困惑。
我认为这可能与此https://github.com/pandas-dev/pandas/issues/1134有关
无论如何,我很好奇我做错了什么以及实现这一目标的最佳方法是什么。
tl;博士:
其中 s
是 pandas.Series 的 int
[i != s[:-1][idx] for idx, i in enumerate(s[1:])] != s[:-1].ne(s[1:]) .tolist()
编辑谢谢大家,阅读下面的一些答案,可能的解决方案是 sum(s.diff().astype(bool)) - 1
但是我仍然很好奇为什么上述解决方案不起作用
最佳答案
您可以利用diff
>>> from pandas import Series
>>> s = Series([1, 2, 1, 3, 3, 1, 1])
>>> s.diff()
0 NaN
1 1.0
2 -1.0
3 2.0
4 0.0
5 -2.0
6 0.0
dtype: float64
>>> s.diff().ne(0) # Same of s.diff() != 0
0 True
1 True
2 True
3 True
4 False
5 True
6 False
dtype: bool
>>> # To know how many times the values had changed simply count the
... # number of True, except the first which is fault of the NaN
... # generated by `diff()` function.
...
>>> sum(s.diff().ne(0)) - 1
4
关于python - Pandas Series.ne 运算符针对同一系列的两个切片返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48935958/
有时,我倾向于(重复)执行next next a 来获取特定元素。当您需要 2 次或更少的遍历时,这很有效。然而,它很快就会变得很麻烦。对于这个简单的情况,循环的开销太大。 幸运的是,如果您知道位置,
我在使用值为 numpy 数组的 pandas.Series 时遇到了以下奇怪的行为。 % s = pd.Series([5,2], index=[6,7]) %s.loc[6] 5 <-- ret
我有一个看起来像这样的数据框(小版本): A B C 0 125 ADB [AF:12] 1 189 ACB [AF:78, AF:85, AF:98] 2 148 ADB
我在 Pandas (s1) 中创建了一个系列,用于根据原始 DataFrame 中的列 ('d1') 计算这些固定数字 (1-14) 的实例数。我想要的显示在这里(时报); s1 Last
pandas series 有两个密切相关的属性:Series.index 和 Series.index.values。 这两个中的第一个返回某些 pandas 索引类型的当前索引。它是可变的,可用于
我正在尝试使用 KNNClassifier 训练模型。我将数据拆分如下: X_train, X_test, y_train, y_test = train_test_split(X_bow, y, t
我只是尝试对我的数据框进行排序并使用了以下函数: df[df.count >= df.count.quantile(.95)] 返回错误: AttributeError: 'function' obj
我试过了 print(type(numbers[2])) numbers[2].tolist() print(type(numbers[2])) 那是行不通的。我得到了 Numbers 是一个矩阵
我想从时间戳中减去日期。settings.dataset_end_date 是一个 pandas._libs.tslibs.timestamps.Timestamp引用['date_of_patent
我有一个带有数据的 pandas.core.series.Series 0 [00115840, 00110005, 001000033, 00116000... 1 [00267285,
s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) ) s 输出 1 3 7 6 2 0 9
pandas.DataFrame.query() 方法非常适合在加载或绘图时(预/后)过滤数据。它对于方法链特别方便。 我发现自己经常想将相同的逻辑应用于 pandas.Series,例如在完成诸如返
这个问题在这里已经有了答案: Difference between map, applymap and apply methods in Pandas (11 个回答) 去年关闭。 Series.ma
我正在总结一系列中的值,但根据我如何做,我会得到不同的结果。我试过的两种方法是: sum(df['series']) df['series'].sum() 为什么它们会返回不同的值? 示例代码。 s
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
这个问题在这里已经有了答案: Pandas: select DF rows based on another DF (5 个答案) 关闭 5 年前。 如果我有一个包含开始时间和结束时间的 DataF
我尝试了 Series.index 和 Series.keys() 并且输出是相似的。我找不到它们之间的显着差异。它们是否适用于某些特殊条件? 我在 Anaconda 上的 Jupyter Noteb
我有一个(非常大的)系列,其中包含关键字(例如,每行包含多个由“-”分隔的关键字 In[5]: word_series Out[5]: 0 the-cat-is-pink 1
我需要使用 pandas.read_excel 通过 Python 获取 Excel 电子表格最后一个单元格的值。该单元格包含一个日期,我需要将其分配给 Python 脚本中的变量。格式为2018-1
我是一名优秀的程序员,十分优秀!