与Python的数据框中的有条件计数

发布于 2025-01-27 03:40:33 字数 379 浏览 2 评论 0原文

我想计算有多少次:

  1. “增加”以“增加
  2. “增加”以“减少”
  3. 增加 ”,从“减少”
  4. “减少”为“不变”
  5. “不变”,以“增加”
  6. “不变”为“降低”
  7. “不变”到“不变”

代码,必须用python编写。

DataFrame showing trend respective to date

I want to count how many times there is:

  1. "Increase" to "Increase"
  2. "Increase" to "Decrease"
  3. "Increase" to "Unchanged"
  4. "Decrease" to "Increase"
  5. "Decrease" to "Decrease"
  6. "Decrease" to "Unchanged"
  7. "Unchanged" to "Increase"
  8. "Unchanged" to "Decrease"
  9. "Unchanged" to "Unchanged"

Code must be written in python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2025-02-03 03:40:34

我正在创建一个示例数据框以解决此问题。

import pandas as pd
import numpy as np
df = pd.DataFrame({'id': np.arange(0,16), "trend": ['in','de', 'in', 
'in','in','un','de','de','un','un','de','de','in','de','in','in']})

第1:列出名为'趋势'的列的列表

val = list(df['trend'])

第二:创建一个新列表,然后将每对元组添加到列表中。每个元组都根据迭代包含第一个值及其连续的下一个值。

f = val[0]
list_trend = []

for x in val[1:]:
    list_trend.append((f,x))
    f = x

输出列表将就像这样

I'm creating a sample dataframe to work on this problem.

import pandas as pd
import numpy as np
df = pd.DataFrame({'id': np.arange(0,16), "trend": ['in','de', 'in', 
'in','in','un','de','de','un','un','de','de','in','de','in','in']})

sample dataframe

1st: Make a list of the column named 'trend'

val = list(df['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.

f = val[0]
list_trend = []

for x in val[1:]:
    list_trend.append((f,x))
    f = x

The output list will be like this ????
list of pairs

3rd: By using 'Counter', you can count occurances of each unique pair from that list.

from collections import Counter
c = Counter(list_trend)
count = c.items()

The output will be like this ????
count of each unique pair

And that's it.....

北凤男飞 2025-02-03 03:40:34

我可以想到一种方法不会是非常有效的,但会完成工作:

因此,基本思想是迭代趋势列。您可以制作(new_df.trend [i],new_df.trend [i+1])之类的元组。然后,您使用计数器来获取词典,这将是这样的:

{("Increase", "Increase"): 131, ("Increase", "Decrease"): 317, ...}

实际的实现应该看起来像这样:

from collections import Counter

list_of_tuples = [] 
for i in range(len(new_df.trend) - 1):
    list_of_tuples.append((new_df.trend[i], new_df.trend[i+1]))
occurrences = dict(Counter(list_of_tuples))

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:

{("Increase", "Increase"): 131, ("Increase", "Decrease"): 317, ...}

The actual implementation should look something like this:

from collections import Counter

list_of_tuples = [] 
for i in range(len(new_df.trend) - 1):
    list_of_tuples.append((new_df.trend[i], new_df.trend[i+1]))
occurrences = dict(Counter(list_of_tuples))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文