久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Pandas、groupby 和特定月份的求和

Pandas, groupby and summing over specific months(Pandas、groupby 和特定月份的求和)
本文介紹了Pandas、groupby 和特定月份的求和的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)數(shù)據(jù)框:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 982 entries, 2009-10-30 00:00:00 to 2012-12-16 00:00:00
Data columns (total 4 columns):
rain        981  non-null values
temp_max    982  non-null values
temp_min    982  non-null values
temp        982  non-null values
dtypes: float64(4)

對(duì)于每年/每月的求和,我使用:

For summing per Year/Month i use :

mdata = data.groupby([lambda x: x.year, lambda x: x.month]).agg([sum])

但我需要季節(jié)性分析(夏季、冬季等),那么我如何創(chuàng)建特定月份的總和,例如每年的 [1 ,2 ,3]?

But i need Seasonal analysis (summer, winter etc), so how i can create the Sum of specific months like [1 ,2 ,3] of each year?

推薦答案

是的,對(duì)我來(lái)說(shuō)似乎很簡(jiǎn)潔的一種解決方案是使用 Seasons 字典,然后使用函數(shù)對(duì)數(shù)據(jù)進(jìn)行分組.作為組鍵傳遞的任何函數(shù),每個(gè)索引值都會(huì)調(diào)用一次,返回值用作組名.

Yes, one solution which seems neat to me is to use a Seasons dictionary and then group the data using a function. Any function passed as a group key is called once per index value and the return values are used as the group names.

import pandas as pd
import numpy as np
from pandas import DataFrame
import datetime
# Create a year's worth of data
base = datetime.date.today() - datetime.timedelta(365)
Datelist = [base + datetime.timedelta(days = x) for x in range(365)]
DF = DataFrame(np.random.rand(365), index = Datelist)

# Create a Seasonal Dictionary that will map months to seasons
SeasonDict = {11: 'Winter', 12: 'Winter', 1: 'Winter', 2: 'Spring', 3: 'Spring', 4: 'Spring', 5: 'Summer', 6: 'Summer', 7: 'Summer', 
8: 'Autumn', 9: 'Autumn', 10: 'Autumn'}

# Write a function that will be used to group the data
def GroupFunc(x):
    return SeasonDict[x.month]

# Call the function with the groupby operation. 
Grouped = DF.groupby(GroupFunc)
Grouped.sum()

該函數(shù)獲取每個(gè)索引值并在季節(jié)字典中查找月份并返回與月份鍵對(duì)應(yīng)的值.該值隨后成為組名.

The function takes each index value and looks up the month in the Seasons Dictionary and returns the value corresponding to the month key. This value then becomes the group name.

或者,您可以使用示例中的 lambda(效率更高,但我認(rèn)為上面的內(nèi)容更容易理解):

Alternatively you can use the lambda as in your example (which is more efficient, but I thought the above would be easier to understand):

DF.groupby(lambda x: SeasonDict[x.month]).sum()

根據(jù)評(píng)論的附加代碼在我看來(lái),您最好對(duì)數(shù)據(jù)進(jìn)行切片.因此,您可以執(zhí)行以下操作

ADDITIONAL CODE AS PER COMMENTS It seems to me like you would be better off slicing the data. So you could do the following

DF['Season'] = ""
for row in DF.index:
    DF.Season[row] = SeasonDict[row.month]
DFWinter = DF[DF.Season == 'Winter']

現(xiàn)在您有了一個(gè)包含冬季數(shù)據(jù)的新數(shù)據(jù)框,可以隨意使用.不同之處在于 groupby 操作允許您對(duì)所有數(shù)據(jù)進(jìn)行相同的操作,而聽(tīng)起來(lái)您想以不同的方式調(diào)查數(shù)據(jù)集不同部分的屬性.為此,最好進(jìn)行切片,在這種情況下使用布爾切片.

Now you have a new data frame with the winter data in, to play with as you desire. The difference is that the groupby operations allow you to undertake the same operations on all the data, whereas it sounds like you wanted to investigate the properties of different parts of your data set in different ways. To do that its better to slice, in this case using Boolean slicing.

這篇關(guān)于Pandas、groupby 和特定月份的求和的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 一区二区不卡视频 | 成人在线免费电影 | 欧美一区二区三区在线观看 | 欧美八区 | 欧美日韩专区 | 99国产精品视频免费观看一公开 | 国产精品久久福利 | 亚洲一区二区免费电影 | 精品视频999 | 91在线看| 91在线视频一区 | 国产精品久久久久久妇女6080 | 日韩成人| 日韩精品 | 国产一区不卡在线观看 | 久久噜噜噜精品国产亚洲综合 | 久久久久免费精品国产小说色大师 | 亚洲一区有码 | 日韩免费看视频 | 国产精品一区久久久久 | 91亚洲精品国偷拍自产在线观看 | 日韩在线免费播放 | 亚洲成人自拍 | 日韩在线精品强乱中文字幕 | 国产亚洲精品久久久久动 | 国产精品乱码一区二三区小蝌蚪 | 久久久久国产一级毛片 | 在线欧美小视频 | 欧美激情在线一区二区三区 | 手机日韩 | 精品伊人 | 在线免费观看a级片 | 91免费高清 | 成人在线免费 | a级免费黄色片 | 日日躁狠狠躁aaaaxxxx | 久久精品国产一区二区三区 | 欧美成人a | 久久99精品久久久久久 | 亚洲欧美精品国产一级在线 | 在线91|