Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以大熊猫为单位,获取每个月datetime项目的平均值_Python_Pandas - Fatal编程技术网

Python 以大熊猫为单位,获取每个月datetime项目的平均值

Python 以大熊猫为单位,获取每个月datetime项目的平均值,python,pandas,Python,Pandas,我有一个大的df,每个月有许多条目。我想看看每个月的平均参赛人数,作为一个例子,看看是否有任何月份通常有更多的参赛人数。(理想情况下,我想用一行总体的平均值来描述这一点进行比较,但这可能是后面的问题)。 我的df是这样的: ufo=pd.read_csv('https://raw.githubusercontent.com/justmarkham/pandas-videos/master/data/ufo.csv') ufo['Time']=pd.to_datetime(ufo.Time) 头

我有一个大的df,每个月有许多条目。我想看看每个月的平均参赛人数,作为一个例子,看看是否有任何月份通常有更多的参赛人数。(理想情况下,我想用一行总体的平均值来描述这一点进行比较,但这可能是后面的问题)。 我的df是这样的:

ufo=pd.read_csv('https://raw.githubusercontent.com/justmarkham/pandas-videos/master/data/ufo.csv')
ufo['Time']=pd.to_datetime(ufo.Time)
头部看起来像这样:

所以,如果我想看看夏天是否有更多的ufo目击事件作为例子,我会怎么做

我试过:

ufo.groupby(ufo.Time.month).mean()
但它只有在我计算一个数值时才起作用。如果我使用
count()


编辑:为了澄清,我想知道每个月的平均参赛人数——ufo目击人数

您的意思是要按月对数据进行分组吗?我想我们可以做到

ufo['month'] = ufo['Time'].apply(lambda t: t.month)
ufo['year'] = ufo['Time'].apply(lambda t: t.year)
这样,您将有“年”和“月”来分组数据

ufo_2 = ufo.groupby(['year', 'month'])['place_holder'].mean()

你可以这样做:

# count the total months in the records
def total_month(x):
    return x.max().year -x.min().year + 1

new_df = ufo.groupby(ufo.Time.dt.month).Time.agg(['size', total_month])

new_df['mean_count'] = new_df['size'] /new_df['total_month']
month   mean_instance
1       12.314286
2       11.671429
3       15.657143
4       14.928571
5       16.685714
6       43.084507
7       33.028169
8       27.436620
9       23.028169
10      24.267606
11      21.253521
12      14.563380
输出:

    size    total_month     mean_count
Time            
1   862     57              15.122807
2   817     70              11.671429
3   1096    55              19.927273
4   1045    68              15.367647
5   1168    53              22.037736
6   3059    71              43.084507
7   2345    65              36.076923
8   1948    64              30.437500
9   1635    67              24.402985
10  1723    65              26.507692
11  1509    50              30.180000
12  1034    56              18.464286

我想这就是你要找的,但如果我没有找到你要找的,请要求澄清

# Add a new column instance, this adds a value to each instance of ufo sighting
ufo['instance'] = 1

# set index to time, this makes df a time series df and then you can apply pandas time series functions.  
ufo.set_index(ufo['Time'], drop=True, inplace=True)   

# create another df by resampling the original df and counting the instance column by Month ('M' is resample by month)
ufo2 = pd.DataFrame(ufo['instance'].resample('M').count())

# just to find month of resampled observation
ufo2['Time'] = pd.to_datetime(ufo2.index.values)

ufo2['month'] = ufo2['Time'].apply(lambda x: x.month)
最后,您可以按月分组:)

这是输出,看起来像这样:

# count the total months in the records
def total_month(x):
    return x.max().year -x.min().year + 1

new_df = ufo.groupby(ufo.Time.dt.month).Time.agg(['size', total_month])

new_df['mean_count'] = new_df['size'] /new_df['total_month']
month   mean_instance
1       12.314286
2       11.671429
3       15.657143
4       14.928571
5       16.685714
6       43.084507
7       33.028169
8       27.436620
9       23.028169
10      24.267606
11      21.253521
12      14.563380

ufo.groupby(ufo.Time.month.size()
?我不知道你的意思。例如,让我们以1930年1月为例。你所说的这个月的“平均参赛人数”是什么意思?你如何手工计算?@pmarcol手工计算我会计算1月份的所有条目,然后除以从df开始到df最后一个条目的“1月份”数。然后是每个月。@QuangHoang,然后我仍然得到每个月所有条目的总和。有没有办法知道每个月有多少人出席?我E从第一个到最后一个条目有多少个一月、二月等?然后用它来划分我从你的命令中得到的序列?你应该把你的澄清放在上面的问题中。什么是“占位符”?它的作用是什么?这意味着我将mean()应用于一个名为“palce_holder”的列,该列在您的数据集中不存在。因此,您可以计算“month”,并创建一个名为“month\u count”的新列。那你应该会得到你想要的结果。是的,这就是我想要的。我想知道是否一个不应该包括所有月份,即使没有目击,所以说,以获得一个统计正确的材料。因为1月有57个,2月有70个,人们会认为它们的区别不会超过一个。你明白我的意思吗?我想这是一个正确的观点。71年记录的平均计数更有意义。