简体   繁体   English

在pandas中分组数据帧?

[英]Grouping dataframes in pandas?

I'm pretty new to python pandas and working with dataframes. 我对python pandas和使用数据帧很新。 Lets say that I have the dataframe shown below : 可以说我的数据框如下所示:

A B C

3 2 3
4 2 4
3 2 1 
5 6 6

I want to find how many rows in my dataframe have the same value in the A and B column and for these rows I want to store the C value. 我想找到数据框中有多少行在A和B列中具有相同的值,对于这些行,我想存储C值。 So for example in this dataframe, I want to store the 1st and 3rd C value, since the A and B in each of these rows have the same values. 因此,例如在此数据框中,我想存储第1和第3个C值,因为每个行中的A和B具有相同的值。 Basically I want a way to print something like: "For A = 3 and B = 2 the possible C values are : 3,1" and find those pairs. 基本上我想要一种打印类似的方法:“对于A = 3和B = 2,可能的C值是:3,1”并找到这些对。 Im following the official pandas documentation but I can't seem to find this. 我遵循官方的熊猫文档,但我似乎无法找到这个。

Not sure if I follow but this might get you going: 不确定我是否遵循,但这可能会让你去:

df = DataFrame({"a": [3,4,3,5], "b":[2,2,2,6], "c": [3,4,1,6]})
In [38]: for i, g in df.groupby(("a", "b")):
             print  i, g["c"].values
         ....:
(3, 2) [3 1]
(4, 2) [4]
(5, 6) [6]

You may try this: 你可以试试这个:

In [187]: df
Out[187]: 
   A  B  C
0  3  2  3
1  4  2  4
2  3  2  1
3  5  6  6

In [188]: df[df.groupby(['A', 'B']).transform(np.count_nonzero).C>=2]
Out[188]: 
   A  B  C
0  3  2  3
2  3  2  1
Name: C, dtype: float64

You can use the unique SeriesGroupBy method : 您可以使用unique SeriesGroupBy方法

In [11]: df.groupby(["A", "B"])["C"].unique()
Out[11]:
A  B
3  2    [3, 1]
4  2       [4]
5  6       [6]
Name: C, dtype: object

See also nunique to get the number of unique elements. 另请参阅nunique以获取唯一元素的数量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM