简体   繁体   English

如何根据另一个数据帧中的条件对一个数据帧中的值进行分组?

[英]How to groupby the values in one dataframe based on a condition in another dataframe?

I have two dataframes whose values are correlated. 我有两个值相关的数据框。 The first one, let's call DF, has some values plues each recording number, like this: 第一个称为DF,每个记录号都有一些值,如下所示:

                   ts  pou     X     Y  value   recording_number
0  1/1/12 12:15:00 AM  NaN     1     3  -0.37       4089
1  1/1/12 12:30:00 AM  NaN     1     3  -0.42       4089
2  1/1/12 12:45:00 AM  NaN     1     3  -0.32       4089
3   1/1/12 1:00:00 AM  NaN     1     3  -0.36       4089
4   1/1/12 1:15:00 AM  NaN     1     3  -0.33       4089
5   1/1/12 1:30:00 AM  NaN     1     3  -0.40       4089
6   1/1/12 1:45:00 AM  NaN     1     3  -0.38       4089
7   1/1/12 2:00:00 AM  NaN     1     3  -0.43       4089
8   1/1/12 2:15:00 AM  NaN     1     3  -0.35       4089
9   1/1/12 2:30:00 AM  NaN     1     3  -0.38       4089

DF is pretty long so that the recording_number is gonna change many times. DF很长,因此recording_number会改变很多次。 The second dataframe, let's call it 'Table', has the recording numbers, as well, plus an id_number. 第二个数据帧,我们称之为“表”,也有记录号,外加一个id_number。

 id_number  recording_number
0         167      9206
1         167      9824
2         167     10890
3         167     10895
4         167     10942
5         167     10944
6         167     11244
7         167     11249
8         167     11567
9         167     11568

Many recording numbers belong to the same id_number. 许多记录号属于相同的id_number。

I would like to groupby all the data in DF belonging to the same id_number in Table. 我想对DF中属于表中相同id_number的所有数据进行分组。 ie to groupby all the data with a specific recording_number in DF which have the same id_number in Table. 即,将DF中具有特定record_number的所有数据分组在一起,这些数据在表中具有相同的id_number。

Thank you very much! 非常感谢你!

Does this work for you ? 这对您有用吗?

df_test = DF.merge(Table, how='inner', right_on='recording_number', left_on='recording_number')

df_test.groupby(by=['recording_number', 'id_number'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据特定条件将数据框中的值替换为另一个数据帧中的值? - How to replace values in a dataframe with values in another dataframe based on certain condition? 如何根据另一个数据框条件替换数据框列中的值 - How to replace the values in a dataframe column based on another dataframe condition 熊猫如何对一个列进行分组并根据另一列的最小唯一值过滤数据框? - How to pandas groupby one column and filter dataframe based on the minimum unique values of another column? Pandas:根据条件将值从一个 dataframe 合并到另一个 - Pandas: Merge values from one dataframe to another based on condition 如何根据另一个 dataframe 中的过滤值聚合一个 dataframe 中的统计信息? - How to aggregate stats in one dataframe based on filtering values in another dataframe? 基于条件的 Pandas DataFrame groupby - Pandas DataFrame groupby based on condition 根据条件将数据框的一列与另一数据框连接 - Join one column of a dataframe with another dataframe based on a condition 如何根据条件添加行与另一个 dataframe - How to add rows based on a condition with another dataframe 根据多个条件过滤一个基于 dataframe 的组 - Filter a dataframe based groupby multiple condition Python dataframe 基于另一列有条件的赋值 - Python dataframe assign values based on another column with condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM