简体   繁体   English

按列中的值分组并获取另一个列的值

[英]Grouping by value in column and getting another columns value

This is the seed DataSet: 这是种子数据集:

In[1]: my_data =
      [{'client':'A','product_s_n':'1','status':'in_store','month':'Jan'}, 
       {'client':'A','product_s_n':'1','status':'sending', 'month':'Feb'}, 
       {'client':'A','product_s_n':'2','status':'in_store','month':'Jan'},
       {'client':'A','product_s_n':'2','status':'in_store','month':'Feb'},
       {'client':'B','product_s_n':'3','status':'in_store','month':'Jan'},
       {'client':'B','product_s_n':'3','status':'sending', 'month':'Feb'},
       {'client':'B','product_s_n':'4','status':'in_store','month':'Jan'},
       {'client':'B','product_s_n':'4','status':'in_store','month':'Feb'},
       {'client':'C','product_s_n':'5','status':'in_store','month':'Jan'},
       {'client':'C','product_s_n':'5','status':'sending', 'month':'Feb'}]
df = pd.DataFrame(my_data)
df

Out[1]:
      client    month   product_s_n   status
0       A       Jan     1             in_store
1       A       Feb     1             sending
2       A       Jan     2             in_store
3       A       Feb     2             in_store
4       B       Jan     3             in_store
5       B       Jan     4             in_store
6       B       Feb     4             in_store
8       C       Jan     5             sending

The question I want to ask this data is: what's the client for each product_serial_number? 我想问这个数据的问题是:每个product_serial_number的客户端是什么? From the data in this example, this is how the resulting DataFrame would look like (I need a new DataFrame as a result): 从此示例中的数据来看,这就是生成的DataFrame的样子(因此,我需要一个新的DataFrame):

    product_s_n    client   
0        1            A
1        2            A
2        3            B
3        4            B
4        5            C

As you may have noticed, the 'status' and 'month' fields are just for 'giving sense' and structure to the data in this sample dataset. 您可能已经注意到,“状态”和“月”字段仅用于“赋予”此样本数据集中的数据和结构。 Tried using groupby, with no success. 使用groupby尝试,没有成功。 Any ideas? 有任何想法吗?

Thanks! 谢谢!

After calling df.groupby(['product_s_n']) you can restrict attention to a particular column by indexing with ['client'] . 调用df.groupby(['product_s_n'])您可以通过使用['client']进行索引来限制对特定列的关注。 You can then select the first value of client from each group by calling first() . 然后,可以通过调用first()从每个组中选择client的第一个值。

>>> df.groupby(['product_s_n'])['client'].first()    
product_s_n
1              A
2              A
3              B
4              B
5              C
Name: client, dtype: object

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

相关问题 在多列上将具有相同列值的行分组 - Grouping rows with same column value on multiple columns 将列值转换为行并根据另一列的值将它们分组 - Transposing column values into rows and grouping them based on value of another column 按列值分组数据 - Grouping data on column value 按列值分组查询集 - Grouping queryset by column value 如何通过按其他多列分组来查找列中值的百分比? - How to find percentage of a value in a column by grouping by other multiple columns? 按第一列中的相同值将数据中的列分组 - Grouping columns from data by same value in first column 如何将带有数字的列更改为反映与前一行列值按另一列值分组相关的变化的列? - How to change column with numbers to column reflecting change in relation to previous row column value grouping by another column value? 在按列A分组并按列B汇总时获取列C的对应值 - Getting the corresponding value of columnC while grouping by column A and aggregating by columnB 基于python中另一个列表的最大值对数组的列索引进行分组 - Grouping column indices of an array based on max value of another list in python 如何将列表列表转换为数据框,保持另一列的分组值? - how to convert a list of list to dataframe keeping the grouping value of another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM