【问题标题】:get the same order after applying group by on data frame在数据框上应用分组后获得相同的顺序
【发布时间】:2018-08-24 18:54:51
【问题描述】:

我有一种情况,在对数据框应用 group by 之前,顺序是升序的,但是在应用数据框之后,顺序发生了变化,并且在内部发生了变化。

这是代码示例: 数据帧: `

final_day_wise = daily_sales_data.loc [ : ,
                         ['Placement#',"Placement# Name" , "Date" , "Delivered Impressions" , "Clicks" , "CTR" , "Conversion" ,
                           "eCPA" , "Spend" ] ]`

值的顺序基于“placement#”列,按升序排列。

输出: 所以在应用 group by 它实际上是在改变相反的顺序或任何其他顺序。

startline = len ( placement_sales_data ) + len ( final_adsize ) + 18
        for placement , placement_df in final_day_wise.groupby ( 'Placement# Name' ):

            writing_daily_data = placement_df.to_excel ( self.config.writer , sheet_name = "Standard banner({})".format (
                self.config.IO_ID ) ,encoding = 'UTF-8' ,startcol = 1 ,
                                              startrow = startline , columns = ["Placement# Name"],index = False ,
                                              header = False , merge_cells = False)
            writing_daily_data_new = placement_df.to_excel ( self.config.writer , sheet_name = "Standard banner({})".format (
                self.config.IO_ID ) , startcol = 1 , startrow = startline+1 ,columns = ["Date","Delivered Impressions","Clicks","CTR",
                                                                                         "Conversion","eCPA","Spend"], index = False , header = True , merge_cells = False)

            startline += len(placement_df) + 5

那么有什么功能或任何东西可以保持顺序不变。

输入。每日销售数据 输出:

【问题讨论】:

  • 您能否发布来自daily_sales_data 的几行代码以及您当前的输出和您想要的输出?
  • @PeterLeimbigler 我已经用图像进行了编辑。请检查

标签: python python-2.7 pandas pandas-groupby


【解决方案1】:

这是你要找的吗?

processed_df = df.groupby('col').agg(...).reset_index()

processed_df.sort_values(['col', 'Placement#'], ascending=True, inplace=True)

【讨论】:

  • 所以重置索引在这里不起作用。我没有应用“agg”功能。直接将reset_index及其请求使用“应用”。
  • 我如何以相同的顺序获取值
猜你喜欢
  • 2020-09-08
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 2022-11-16
  • 2019-03-20
相关资源
最近更新 更多