与Python的数据框中的有条件计数
I want to count how many times there is:
- "Increase" to "Increase"
- "Increase" to "Decrease"
- "Increase" to "Unchanged"
- "Decrease" to "Increase"
- "Decrease" to "Decrease"
- "Decrease" to "Unchanged"
- "Unchanged" to "Increase"
- "Unchanged" to "Decrease"
- "Unchanged" to "Unchanged"
Code must be written in python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在创建一个示例数据框以解决此问题。
第1:列出名为'趋势'的列的列表
第二:创建一个新列表,然后将每对元组添加到列表中。每个元组都根据迭代包含第一个值及其连续的下一个值。
输出列表将就像这样
I'm creating a sample dataframe to work on this problem.
1st: Make a list of the column named 'trend'
2nd: Create a new list and add each pair of tuples into the list. Each tuple contains the first value and its consecutive next value according to the iteration.
The output list will be like this ????

3rd: By using 'Counter', you can count occurances of each unique pair from that list.
The output will be like this ????

And that's it.....
我可以想到一种方法不会是非常有效的,但会完成工作:
因此,基本思想是迭代趋势列。您可以制作
(new_df.trend [i],new_df.trend [i+1])
之类的元组。然后,您使用计数器来获取词典,这将是这样的:实际的实现应该看起来像这样:
I can think of one approach which is not going to be very efficient but will get the job done:
So the basic idea is to iterate over the trend column. You go making tuples like
(new_df.trend[i], new_df.trend[i+1])
. Then you use the counter to get a dictionary which would be something like this:The actual implementation should look something like this: