subset

Fastest way to remove subsets of lists from a list in Python

Fastest way to remove subsets of lists from a list in Python Question: Suppose I have a list of lists like the one below (the actual list is much longer): fruits = [[‘apple’, ‘pear’], [‘apple’, ‘pear’, ‘banana’], [‘banana’, ‘pear’], [‘pear’, ‘pineapple’], [‘apple’, ‘pear’, ‘banana’, ‘watermelon’]] In this case, all the items in the lists [‘banana’, …

Total answers: 3

Subsetting a 2D numpy array

Subsetting a 2D numpy array Question: I have looked into documentations and also other questions here, but it seems I have not got the hang of subsetting in numpy arrays yet. I have a numpy array, and for the sake of argument, let it be defined as follows: import numpy as np a = np.arange(100) …

Total answers: 5

R to Python subsetting via vector

R to Python subsetting via vector Question: I’m a python newbie but have some R experience. In R if I’d like to subset a data.frame I can use a variable to do something like this: # Columns # Assign column names to variable colsToUse <- c(‘col1′,’col2′,’col3’) # Use variable to subset df2 <- df1[,colsToUse] # …

Total answers: 1

Calculating combinations of subsets of given size leading up to the set

Calculating combinations of subsets of given size leading up to the set Question: I would like to get all the possible subsets of a given length, that lead up to the real set. So for the set [a,b,c] and subset sizes 1 & 2 I would like to calculate: [[a,b],[c]] , [[b,c],[a]] , [[a,c],[b]] I’ve …

Total answers: 2

Filter pandas data frame for col == None

Filter pandas data frame for col == None Question: I have a data frame data_df with multiple columns, one of which is c which holds country names. How do I filter out the rows where c == None. My first attempt was to do this: countries_df = data_df[data_df.c != None] However, that yielded 0 rows. …

Total answers: 2

numpy random.choice elements that are not selected

numpy random.choice elements that are not selected Question: I have an array A as below: import numpy as np A = np.random.sample(100) I want to create 2 random subsets from A, that if I combine them together I will get A inx = np.random.choice(np.arange(100), size=70, replace=False) S1 = A[inx] So, S1 is one of the …

Total answers: 3

creating a new list with subset of list using index in python

creating a new list with subset of list using index in python Question: A list: a = [‘a’, ‘b’, ‘c’, 3, 4, ‘d’, 6, 7, 8] I want a list using a subset of a using a[0:2],a[4], a[6:], that is I want a list [‘a’, ‘b’, 4, 6, 7, 8] Asked By: user2783615 || Source …

Total answers: 4

subsetting a Python DataFrame

subsetting a Python DataFrame Question: I am transitioning from R to Python. I just began using Pandas. I have an R code that subsets nicely: k1 <- subset(data, Product = p.id & Month < mn & Year == yr, select = c(Time, Product)) Now, I want to do similar stuff in Python. this is what …

Total answers: 5

Regular expression to filter list of strings matching a pattern

Regular expression to filter list of strings matching a pattern Question: I use R a lot more and it is easier for me to do it in R: > test <- c(‘bbb’, ‘ccc’, ‘axx’, ‘xzz’, ‘xaa’) > test[grepl("^x",test)] [1] "xzz" "xaa" But how to do it in python if test is a list? P.S. I …

Total answers: 6

Test if set is a subset, considering the number (multiplicity) of each element in the set

Test if set is a subset, considering the number (multiplicity) of each element in the set Question: I know I can test if set1 is a subset of set2 with: {‘a’,’b’,’c’} <= {‘a’,’b’,’c’,’d’,’e’} # True But the following is also True: {‘a’,’a’,’b’,’c’} <= {‘a’,’b’,’c’,’d’,’e’} # True How do I have it consider the number of …

Total answers: 5