我有一个条件,并且不能使用if语句来计算单元格条件。

huangapple go评论56阅读模式
英文:

I Have one for condition, and can't do if statement to count cell condition

问题

我试图制定这个条件来计算小于300秒(5分钟)的值(单元格)出现了多少次,但它返回了一个错误。条件是关于下面的一个DF,我从我的csv中提取的:

df =
	0
0	NaN
1	79.0
2	140.0
3	131.0
4	72.0
...	...
16341	349.0
16342	795.0
16343	787.0
16344	410.0
16345	1221.0

我尝试这样做:

for key, value in data_df.items() :
  df = data_df[key].fillna(0, inplace=False).astype(int)   
  if key <= 300 :
   df_count = value.count( value.values() <= 300)
   display(df_count)

但错误信息说:

TypeError: 'numpy.ndarray' object is not callable

我该如何解决我的 IF 语句以执行 Display() 代码?

编辑以获得更好的结果

我应该在这之前询问,但我以为解决办法类似于 IF 语句。

我需要将其制定为范围,条件是计算每个范围内的每个单元格,大约是5到5分钟,直到我完成一个小时。所以条件大致是:

  1. 0到300(5分钟)
  2. 300到600(5到10分钟)
  3. 600到900(10到15分钟)

直到我完成一个小时。我尝试过:
df_count300to600 = data_df[key].fillna(0).le( key > 300 && key< 600 )

在这一点上我该如何解决?

英文:

I'm trying to do this condition to count how many times (cells) have values less than 300 seconds (5 minutes) but it returns me an error, the condition is about a DF below, that I pull from my csv:

df =
	0
0	NaN
1	79.0
2	140.0
3	131.0
4	72.0
...	...
16341	349.0
16342	795.0
16343	787.0
16344	410.0
16345	1221.0

I try this way:

for key, value in data_df.items() :
  df = data_df[key].fillna(0, inplace=False).astype(int)   
  if key <= 300 :
   df_count = value.count( value.values() <= 300)
   display(df_count)

but the error message says:

> TypeError: 'numpy.ndarray' object is not callable

How can I solve my IF statement to execute the Display() code?

EDITED FOR BETTER RESULTS

I Should asked this before but i thought that the sollution would be alike IF statement.

I need to make it in range, the condition is to count every cell in a range about 5 to 5 minutes, untill I get 1 hour complete, So the conditions is about:

  1. 0 to 300 (5minutes)
  2. 300 to 600 (5 to 10 minutes)
  3. 600 to 900 (10 minutes to 15 minutes)

.
.
.

until i get one hour complete, I tryed:
df_count300to600 = data_df[key].fillna(0).le( key > 300 && key< 600 )

How can I solve at this point?

答案1

得分: 0

理解了,你只需要这些代码的翻译:

count = df[0].fillna(0).le(300).sum()
pd.cut(df[0].fillna(0), bins=[0, 300, 600, 900, 1200, 1500]).value_counts()
df[0].fillna(0).floordiv(300).add(1).mul(300).value_counts()
英文:

IIUC, you just need:

count = df[0].fillna(0).le(300).sum()

Example output: 5

How it works

df[0].fillna(0).le(300) creates a boolean Series, sum counts the True:

            0  df['0'].fillna(0).le(300)
0         NaN                       True
1        79.0                       True
2       140.0                       True
3       131.0                       True
4        72.0                       True
16341   349.0                      False
16342   795.0                      False
16343   787.0                      False
16344   410.0                      False
16345  1221.0                      False

edited question:

You might need to use cut:

pd.cut(df[0].fillna(0), bins=[0, 300, 600, 900, 1200, 1500]).value_counts()

Output:

(0, 300]        4
(300, 600]      2
(600, 900]      2
(1200, 1500]    1
(900, 1200]     0
Name: 0, dtype: int64

Or with arithmetics:

df[0].fillna(0).floordiv(300).add(1).mul(300).value_counts()

Output:

300.0     5
600.0     2
900.0     2
1500.0    1
Name: 0, dtype: int64

huangapple
  • 本文由 发表于 2023年3月20日 22:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定