unique

Function to highlight unique observations within groups

Function to highlight unique observations within groups Question: I would like help creating a function that dynamically iterates through a dataframe by group (non specified color), looks at ID to see if which id’s do not line up with the majority of the id’s that are in each grouping by color(so whatever number of observations …

Total answers: 1

Getting first n unique elements from Python list

Getting first n unique elements from Python list Question: I have a python list where elements can repeat. >>> a = [1,2,2,3,3,4,5,6] I want to get the first n unique elements from the list. So, in this case, if i want the first 5 unique elements, they would be: [1,2,3,4,5] I have come up with …

Total answers: 13

How to handle unique data in SQLAlchemy, Flask, Python

How to handle unique data in SQLAlchemy, Flask, Python Question: How do you usually handle unique database entries in Flask? I have the following column in my db model: bank_address = db.Column(db.String(42), unique=True) The problem is, that even before I can make a check whether it is already in the database or not, I get …

Total answers: 2

Boolean for unique value in a column

Boolean for unique value in a column Question: For my dataframe, e.g. df = pd.DataFrame([1, 3, 7, 1], columns=[‘data’]) I want to know for each index if the value is unique in the column data. So the resulting dataframe should be data is_unique 0 1 False 1 3 True 2 7 True 3 1 False …

Total answers: 4

Unique values of two columns for pandas dataframe

Unique values of two columns for pandas dataframe Question: Suppose I have pandas data frame with 2 columns: df: Col1 Col2 1 1 1 2 1 2 1 2 3 4 3 4 Then I want to keep only the unique couple values (col1, col2) of these two columns and give their frequncy: df2: Col1 …

Total answers: 2

Count unique values per groups with Pandas

Count unique values per groups with Pandas Question: I need to count unique ID values in every domain. I have data: ID, domain 123, ‘vk.com’ 123, ‘vk.com’ 123, ‘twitter.com’ 456, ‘vk.com’ 456, ‘facebook.com’ 456, ‘vk.com’ 456, ‘google.com’ 789, ‘twitter.com’ 789, ‘vk.com’ I try df.groupby([‘domain’, ‘ID’]).count() But I want to get domain, count vk.com 3 twitter.com …

Total answers: 4

Drop duplicates using pandas groupby

Drop duplicates using pandas groupby Question: In the dataframe below, I would like to eliminate the duplicate cid values so the output from df.groupby(‘date’).cid.size() matches the output from df.groupby(‘date’).cid.nunique(). I have looked at this post but it does not seem to have a solid solution to the problem. df = pd.read_csv(‘https://raw.githubusercontent.com/108michael/ms_thesis/master/crsp.dime.mpl.df’) df.groupby(‘date’)[‘cid’].agg([‘size’, ‘nunique’]) size nunique …

Total answers: 2

Django REST Framework : "This field is required." with required=False and unique_together

Django REST Framework : "This field is required." with required=False and unique_together Question: I want to save a simple model with Django REST Framework. The only requirement is that UserVote.created_by is set automatically within the perform_create() method. This fails with this exception: { “created_by”: [ “This field is required.” ] } I guess it is …

Total answers: 5

Randomly chose an element of one list that's NOT in a second list

Randomly chose an element of one list that's NOT in a second list Question: Say I have a list2 of randomly chosen elements from a large list1. Is there a clever way of choosing an element from list1 that’s NOT already in list2? For example: list1 = range(20,100) list2 = [37,49,22,35,72] # could be much …

Total answers: 5

Find the unique values in a column and then sort them

Find the unique values in a column and then sort them Question: I have a pandas dataframe. I want to print the unique values of one of its columns in ascending order. This is how I am doing it: import pandas as pd df = pd.DataFrame({‘A’:[1,1,3,2,6,2,8]}) a = df[‘A’].unique() print a.sort() The problem is that …

Total answers: 9