【问题标题】:Python Pandas - Get the rows of first and last day of particular monthsPython Pandas - 获取特定月份的第一天和最后一天的行
【发布时间】:2020-06-22 17:15:08
【问题描述】:

我的数据集df如下:

Date         Value
...
2012-07-31   61.9443
2012-07-30   62.1551
2012-07-27   62.3328
...          ... 
2011-10-04   48.3923
2011-10-03   48.5939
2011-09-30   50.0327
2011-09-29   51.8350
2011-09-28   50.5555
2011-09-27   51.8470
2011-09-26   49.6350
...          ...
2011-08-03   61.3948
2011-08-02   61.5476
2011-08-01   64.1407
2011-07-29   65.0364
2011-07-28   65.7065
2011-07-27   66.3463
2011-07-26   67.1508
2011-07-25   67.5577
...          ...
2010-10-05   57.3674
2010-10-04   56.3687
2010-10-01   57.6022
2010-09-30   58.0993
2010-09-29   57.9934

下面是两列的数据类型:

Type                 Column Name              Example Value
-----------------------------------------------------------------
datetime64[ns]       Date                     2020-06-19 00:00:00
float64              Value                    108.82

我想要一个 df 的子集,它只包含 10 月份的第一个条目最后一个条目的行七月被选中:

Date         Value
...
2012-07-31   61.9443
2011-10-03   48.5939
2011-07-29   65.0364
2010-10-01   57.6022

知道怎么做吗?

【问题讨论】:

  • 如果您只想要 10 月的第一个和 7 月的最后一个,为什么您的输出中每个月都有 2 个整体?
  • 对于我给定的数据框,我希望每年都有 7 月的最后一个条目和 10 月的第一个条目。

标签: python pandas pandas-groupby dayofmonth


【解决方案1】:

您可以按日期排序,以便知道它们是按时间顺序排列的。之后创建两个数据框,一个月份为 7 的数据框取组的最后一条记录,一个月份为 10 的数据框取组的第一条记录。

然后你可以连接它们。

df['Date'] = pd.to_datetime(df['Date'])
df = df.sort_values(by='Date')

j = df[df['Date'].dt.month == 7].groupby([df.Date.dt.year, df.Date.dt.month]).last()
o = df[df['Date'].dt.month == 10].groupby([df.Date.dt.year, df.Date.dt.month]).first()

pd.concat([j,o]).reset_index(drop=True)

输出

    Date        Value
0   2011-07-29  65.0364
1   2012-07-31  61.9443
2   2010-10-01  57.6022
3   2011-10-03  48.5939

【讨论】:

    【解决方案2】:

    这是一个仅基于 Pandas 的解决方案:

    df = df.sort_values("Date")
    october = df.groupby([df["Date"].dt.year, df["Date"].dt.month], as_index = False).first()
    october = october[october.Date.dt.month == 10]
    
    july = df.groupby([df["Date"].dt.year, df["Date"].dt.month], as_index = False).last()
    july = july[july.Date.dt.month == 7]
    
    pd.concat([july, october])
    

    结果是:

            Date    Value
    2 2011-07-29  65.0364
    6 2012-07-31  61.9443
    1 2010-10-01  57.6022
    5 2011-10-03  48.5939
    

    【讨论】:

      【解决方案3】:

      仅使用已排序数据帧中的索引的无组优雅解决方案:

         # Sort you data by Date and convert date string to datetime
         df['Date']=pd.to_datetime(df['Date'])
         df = df.sort_values(by='Date')
         # For selecting first row just subset by index where month is 7 and select first index i.e. 0
         jul = df.loc[[df.index[df['Date'].dt.month == 7].tolist()[0]]]
         # For sleecting last row just subset by index where months is 10 and select last index i.e -1
         oct = df.loc[[df.index[df['Date'].dt.month == 10].tolist()[-1]]]
         #Finally concatenate both    
         pd.concat([jul,oct]).reset_index(drop=True)
      
      
        
      

      【讨论】:

        猜你喜欢
        • 2011-11-24
        • 2019-03-20
        • 2017-10-29
        • 2014-03-22
        • 1970-01-01
        • 2014-11-21
        • 2017-01-09
        • 2019-03-04
        • 1970-01-01
        相关资源
        最近更新 更多