简体   繁体   English

如何从数据帧计算事件的相对频率?

[英]How to calculate relative frequency of an event from a dataframe?

I have a dataframe with temperature data for a certain period.我有一个包含一定时期温度数据的数据框。 With this data, I want to calculate the relative frequency of the month of August being warmer than 20° as well as January being colder than 2°.有了这些数据,我想计算 8 月份比 20° 暖和以及 1 月份比 2° 冷的相对频率。 I have already managed to extract these two columns in a separate dataframe, to get the count of each temperature event and used the normalize function to get the frequency for each value in percent (see code).我已经设法在单独的数据框中提取这两列,以获取每个温度事件的计数,并使用 normalize 函数以百分比形式获取每个值的频率(参见代码)。

df_temp1[df_temp1.aug >=20]
df_temp1[df_temp1.jan <= 2]

df_temp1['aug'].value_counts()
df_temp1['jan'].value_counts()

df_temp1['aug'].value_counts(normalize=True)*100
df_temp1['jan'].value_counts(normalize=True)*100

What I haven't managed is to calculate the relative frequency for aug>=20, jan<=2, as well as aug>=20 AND jan<=2 and aug>=20 OR jan<=2.我没有设法计算 aug>=20、jan<=2 以及 aug>=20 AND jan<=2 和 aug>=20 OR jan<=2 的相对频率。 Maybe someone could help me with this problem.也许有人可以帮助我解决这个问题。 Thanks.谢谢。

I would try something like this:我会尝试这样的事情:

proprortion_of_augusts_above_20 = (df_temp1['aug'] >= 20).mean()
proprortion_of_januaries_below_20 = (df_temp1['jan'] <= 2).mean()

This calculates it in two steps.这分两步计算。 First, df_temp1['aug'] >= 20 creates a boolean array, with True representing months above 20, and False representing months which are not.首先, df_temp1['aug'] >= 20创建一个布尔数组,其中 True 表示大于 20 的月份,而 False 表示不大于 20 的月份。

Then, mean() reinterprets True and False as 1 and 0. The average of this is the percentage of months which fulfill the criteria, divided by 100.然后,mean() 将 True 和 False 重新解释为 1 和 0。其平均值是满足条件的月份百分比除以 100。

As an aside, I would recommend posting your data in a question, which allows people answering to check whether their solution works. 顺便说一句,我建议将您的数据发布在一个问题中,以便人们回答以检查他们的解决方案是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM