如何使用GROUPBY获取唯一标识的累计和?

2024-05-13 22:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和pandas非常陌生,他们正在开发一个pandas数据帧,它看起来像

Date     Time           ID   Weight
Jul-1     12:00         A       10
Jul-1     12:00         B       20
Jul-1     12:00         C       100
Jul-1     12:10         C       100
Jul-1     12:10         D       30
Jul-1     12:20         C       100
Jul-1     12:20         D       30
Jul-1     12:30         A       10
Jul-1     12:40         E       40
Jul-1     12:50         F       50
Jul-1     1:00          A       40

我试图实现按日期、时间和id分组,并应用累积和,这样如果id在下一个时隙中出现,则权重只添加一次(唯一)。生成的数据帧如下所示

^{pr2}$

这是我在下面尝试的方法,但仍在多次计算权重:

df=df.groupby(['date','time','ID'])['Wt'].apply(lambda x: x.unique().sum()).reset_index()
df['cumWt']=df['Wt'].cumsum()

任何帮助都将非常感谢!在

提前多谢了!!在


Tags: 数据idpandasdfdatetime时间jul
1条回答
网友
1楼 · 发布于 2024-05-13 22:19:59

下面的代码使用pandas.duplicate()pandas.merge()pandas.groupby/sum和{a4}来获得所需的输出:

# creates a series of weights to be considered and rename it to merge
unique_weights = df['weight'][~df.duplicated(['weight'])]
unique_weights.rename('consider_cum', inplace = True)

# merges the series to the original dataframe and replace the ignored values by 0
df = df.merge(unique_weights.to_frame(), how = 'left', left_index=True, right_index=True)
df.consider_cum = df.consider_cum.fillna(0)

# sums grouping by date and time
df = df.groupby(['date', 'time']).sum().reset_index()

# create the cumulative sum column and present the output
df['weight_cumsum'] = df['consider_cum'].cumsum()
df[['date', 'time', 'weight_cumsum']]

生成以下输出:

enter image description here

相关问题 更多 >