Repeat each row of data.frame the number of times specified in a column

df <- data.frame(var1 = c(‘a’, ‘b’, ‘c’), var2 = c(‘d’, ‘e’, ‘f’), freq = 1:3) What is the simplest way to expand each row the first two columns of the data.frame above, so that each row is repeated the number of times specified in the column ‘freq’? In other words, go from this: df var1 … Read more

Compare two data.frames to find the rows in data.frame 1 that are not present in data.frame 2

I have the following 2 data.frames: a1 <- data.frame(a = 1:5, b=letters[1:5]) a2 <- data.frame(a = 1:3, b=letters[1:3]) I want to find the row a1 has that a2 doesn’t. Is there a built in function for this type of operation? (p.s: I did write a solution for it, I am simply curious if someone already … Read more

How to access pandas groupby dataframe by key

How do I access the corresponding groupby dataframe in a groupby object by the key? With the following groupby: rand = np.random.RandomState(1) df = pd.DataFrame({‘A’: [‘foo’, ‘bar’] * 3, ‘B’: rand.randn(6), ‘C’: rand.randint(0, 20, 6)}) gb = df.groupby([‘A’]) I can iterate through it to get the keys and groups: In [11]: for k, gp in … Read more