我如何计算标志列?
我如何计算每行重复的正或负元素的数量?
假设我有以下数据:
ski 2020 2021 2022 2023 2024 2025
book 1.2 5.6 8.4 -2 -5 6
jar 4.2 -5 -8 2 4 6
kook -4 -5.2 -2.3 -5.6 -7 8
输出是每行的列表,它计算相似符号的数量。例如,在第一行中,我们有3个正元素,然后有2个负面元素,而1个正元素。因此输出为[3,-2,1]。对于其他2行,输出如下:
jar [1,-2,3]
kook [-5,1]
How I can count the number of repetitive positive or negative elements in each row?
Suppose I have the following data:
ski 2020 2021 2022 2023 2024 2025
book 1.2 5.6 8.4 -2 -5 6
jar 4.2 -5 -8 2 4 6
kook -4 -5.2 -2.3 -5.6 -7 8
The output is a list for each row that counts the number of similar signs. For example in the first row we have 3 positive elements and then 2 negative and again one positive. So the output is [3,-2,1]. and for 2 other rows the output is as follows:
jar [1,-2,3]
kook [-5,1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 使用自定义lambda function 对于计数串联值:
Use
DataFrame.clip
with custom lambda function for count consecutive values:让我们尝试:
Let us try:
使用:
输出:
Use:
Output:
我认为 ski 是索引列。如果不是,请将其设置为索引,将当前的索引设置为索引。
从定义一个函数开始,要应用于每一行:
然后应用它:
对于您的源数据,我得到:
我的解决方案比另一个要短得多。
I assume that ski is the index column. If not, set it as the index, dropping the current one.
Start from defining a function, to be applied to each row:
Then apply it:
For your source data, I got:
My solution is significantly shorter than the other.