【发布时间】:2021-05-30 06:53:16
【问题描述】:
似乎有很多关于 Dataframe groupby() 方法的在线示例,它似乎描述了按列分组和跨多行(系列)的数据,即“从上到下”
给定 2 个数据帧 df_1 和 df_2:
df_1:
Instru_1 Instru_2 Instru_3 Instru_5 Instru_6 Instru_7
2020-10-01 10 10 20 20 10 30
其中行值是分类 ID 和
df_2:
Instru_1 Instru_2 Instru_3 Instru_5 Instru_6 Instru_7
2020-10-01 0.1 0.2 0.2 0.2 0.2 0.1
其中行值是权重总和为 1.0
如果我需要跨 df_1 的行(值)进行分组,其中仪器的数量可能不确定,那么 groupby() 是否仍然是前进的方向,以获得结果 df_result:
df_result:
10 20 30
2020-10-01 0.5 0.4 0.1
where: The columns are the classification IDs from df_1 record
The values are the sum for each classification ID from df_2
(例如,分类 ID=10,元素值 = 0.1 + 0.2 + 0.2 = 0.5,分类 ID=20,元素 = 0.2 + 0.2 = 0.4 和 ID=30,元素 = 0.1)
Is the quickest still to to perform multiple steps (merge df_1 and df_2 and process per row) ?
Step 1: Enum row 1 classification Ids and create df_result
Step 2: Enum row 2 and perform the summation per classification (this looks tricky!)
任何关于最佳方法的建议将不胜感激..(或指向跨行值分组的指针..)在此先感谢..
【问题讨论】: