[英]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.