Home > Back-end >  'SeriesGroupBy' object has no attribute 'is_unique'
'SeriesGroupBy' object has no attribute 'is_unique'

Time:02-26

  • Operating System: Windows 10
  • python: 3.7.11
  • IDE: jupyter notebook

I have a dataset with the four following columns: bug_report_number, class_id, time_stamp, label. The dataset is something like bellow:

41737   120098  1583149803  0
41737   120116  1583149803  0
41737   120136  1583149803  0
41748   120179  1583135020  0
41748   120177  1583135020  -1
41748   120177  1583135020  -1
41754   120177  1583135020  1
41754   120200  1583135020  0
41754   120188  1583135020  0

I want to groupby bug_report_number and then check if the class_id column values are unique for that bug report or not.

For example, for 41748 bug_report_number I expect to get False, and for 41754 I expect to get True.

The code I wrote is as follows:

import pandas as pd
train_file_path = "dataset_hbase - v.03.csv"
columns_name = ["bug_report_number", "class_id", "time_stamp", "label"]
columns_dtype = {0: "int64", 1: "int64", 2: "int64", 3:"int64"}
df = pd.read_csv(train_file_path, header=None, names=columns_name, dtype=columns_dtype)

temp = df.groupby(["bug_report_number"])
temp["class_id"].is_unique

but when I use .is_unique it returns the following error:

AttributeError: 'SeriesGroupBy' object has no attribute 'is_unique'

Question:

  • How to groupby bug_report_number and then check if the class_id column values are unique for that bug report or not?

CodePudding user response:

Use:

data = pd.DataFrame({'bug_report_number': [1,2,1,2,1], 'id': [50,35,50,30,50]})
df =  pd.DataFrame(data)
df.groupby('bug_report_number')['id'].apply(lambda x: 0 if len(list(x))==len(set(x)) else 1)

Output:

enter image description here

CodePudding user response:

IIUC, you could use groupby nunique eq(1). The idea is to count the number of unique "class_id"s for each "bug_report_number" and return True if it's equal to 1 False otherwise.

s = df.groupby('bug_report_number')['class_id'].nunique()
out =  s.eq(1) 

Output:

bug_report_number
41737    False
41748    False
41754    False
Name: class_id, dtype: bool
  • Related