add rows to dataframe after multiply by column value [duplicate] - python-3.x

This question already has an answer here:
Repeat Rows in Data Frame n Times [duplicate]
(1 answer)
Closed 5 months ago.
I have the following datafame:
count color name
2 blue mike
3 red james
1 orange mark
3 yellow oscar
I want to take the count value and insert as many rows as the count value. The output should be:
count color name
2 blue mike
2 blue mike
3 red james
3 red james
3 red james
1 orange mark
3 yellow oscar
3 yellow oscar
3 yellow oscar
I've tried to use the multiply function, but this is just multiplying the count column when I want to insert more rows. If someone could point me in the right direction, I would greatly appreciate it.

Try with reindex and repeat
df = df.reindex(df.index.repeat(df['count']))

Related

EXCEL Get top 3 largest numbers in repetitive array

enter image description hereI have an array of people with scores in other column. I need to find top 3 people with highest score and print their names.
Example:
Maria 1
Thomas 4
John 3
Jack 2
Ray 2
Laura 4
Kate 3
Result should be:
Thomas
Laura
John
What I get:
Thomas
Thomas
John
What I get:
Thomas
John
num
I have tried using LARGE, MATCH, MIN, MAX but nothings works.
My first failure code:
=INDEX($A$2:$A$8; MATCH(LARGE(($B$2:$B$8);{1;2;3}); $B$2:$B$8;0))
My second failure code:
{=INDEX($A$2:$A$14;SMALL(IF($B$2:$B$14=MAX($B$2:$B$14);ROW($B$2:$B$14)-1);ROW(B4)-1))}
Put this in the second row of the column you want:
=INDEX(A:A,AGGREGATE(15,7,ROW($B$1:$B$7)/((COUNTIF($D$1:D1,$A$1:$A$7)=0)*($B$1:$B$7=LARGE(B:B,ROW(1:1)))),1))
And drag down three rows:

Concatenating INDEX/MATCH with multiple criteria and multiple matches

I am using Excel to track a team game where players are divided into teams and subteams within teams. Each player within a subteam scores a certain number of points, and I would like to have a summary string for each player with the number of points other players in the same subteam scored.
Example:
A B C D
PLAYER TEAM SUBTEAM POINTS
Alice Red 1 70
Bob Red 1 20
Charlie Red 1 10
Dave Red 2 70
Erin Red 2 30
Frank Blue 1 55
Grace Blue 1 45
My desired output looks like this:
A B C D E
PLAYER TEAM SUBTEAM POINTS SUMMARY
Alice Red 1 70 Bob:20, Charlie:10
Bob Red 1 20 Alice:70, Charlie:10
Charlie Red 1 10 Alice:70, Bob:20
Dave Red 2 70 Erin:30
Erin Red 2 30 Dave:70
Frank Blue 1 55 Grace:45
Grace Blue 1 45 Frank:55
The furthest I was able to go is a combination of CONCATENATE, INDEX, and MATCH in an array formula:
{=CONCATENATE(INDEX($A$2:$A$8,MATCH(1,(C2=$C$2:$C$8)*(B2=$B$2:$B$8),0)), ":", INDEX($D$2:$D$8,MATCH(1,(C2=$C$2:$C$8)*(B2=$B$2:$B$8),0)))}
This unfortunately just outputs a summary for the first player in the subteam:
A B C D E
PLAYER TEAM SUBTEAM POINTS SUMMARY
Alice Red 1 70 Alice:70
Bob Red 1 20 Alice:70
Charlie Red 1 10 Alice:70
Dave Red 2 70 Dave:70
Erin Red 2 30 Dave:70
Frank Blue 1 55 Grace:45
Grace Blue 1 45 Grace:45
What I need to do now is:
Excluding the player for the summary (I don't want Alice in the summary for Alice, but only Bob and Charlie)
Getting it to work for multiple matches (there can be an arbitrary number of players in each subteam)
Getting CONCATENATE to work with an unknown number of strings (because as said above, there can be an arbitrary number of players in each subteam).
Ideas appreciated!
I put together a helper column that concatenates each player/points and the TEXTJOINIFS from TEXTJOIN for xl2010/xl2013 with criteria for the desired results.
Unfortunately Excel (prior to Excel 2016) cannot conveniently join text. The best you can do (if you want to avoid VBA) is to use some helper cells and split this "Summary" into separate cells.
See example below. The array formula in cell E4 is dragged to cell J10.
= IFERROR(INDEX($A$4:$D$10,MATCH(SMALL(IF(($B$4:$B$10=$B4)*($C$4:$C$10=$C4)*($A$4:$A$10<>$A4),
ROW($A$4:$A$10)),E$3),ROW($A$4:$A$10),0),MATCH(E$2,$A$1:$D$1,0)),"")
Note this is an array formula, so you must press Ctrl+Shift+Enter instead of just Enter after typing this formula.
Of course, in this example I assume 3 players. Your requirement of arbitrary amount of players cannot be met with formulas alone, but you can just extend the "Summary" section over to the right as far as necessary.
If you really wanted to, you could even concatenate the "Summary" rows to form a single cell, e.g. something like:
= CONCATENATE(E4,": ",F4,", ",...)

Excel sort by similar cell data

So I have this list of data that i need to compare 2 spreadsheets with. Im going to simplify it with a list like below(Column A being a part number, and column B being a quantity):
Spreadsheet 1:
Red 1
Blue 2
Green 1
Orange 6
Yellow 8
Spreadsheet 2:
Red 1
Green 1
Blue 2
Orange 6
Yellow 8
Silver 2
Brown 3
Now what i would like my output to be:
Red 1
Blue 2
Green 1
Orange 6
Yellow 8
Silver 2
Brown 3
Notice that im sorting it so that list 2 aligns with list one, and if list 2 contains things that are not on list 1 it puts it at the bottom(preferably vice-versa compatible). Im not sure if this is even possible, but if it is it will GREATLY decrease my workload so any help is MUCH appreciated. Thanks for your time!

Excel Dynamic Chart

I would like to create a dynamic chart. For example, I would like to plot the Apple and Banana series dynamically using the table below (Table).
A B C D E
1 X Y Z Min/Max Row
2 1 1 Apple Apple Min 2
3 2 2 Apple Apple Max 3
4 3 3 Banana Banana Min 4
5 4 4 Banana Banana Max 6
6 5 5 Banana
Thus far, I have created 2 formulas using the OFFSET function as shown below:
=OFFSET(Sheet1!$A$2,0,0,Sheet1!$E$3-Sheet1!$E$2+1)
=OFFSET(Sheet1!$B$2,0,0,Sheet1!$E$3-Sheet1!$E$2+1)
This will plot the Apple series, but next I would like to plot the Banana series and I'm not sure how to modify the OFFSET Reference to start at the correct row.
What I want are formulas like this but with proper OFFSET Reference syntax:
=OFFSET(Sheet1!$A$**Sheet1!$E$4**,0,0,Sheet1!$E$5-Sheet1!$E$4+1)
=OFFSET(Sheet1!$B$**Sheet1!$E$4**,0,0,Sheet1!$E$5-Sheet1!$E$4+1)
Also, if you know of a more elegant way to define the OFFSET Height, please share.
Thanks in advance for your help!
Cheers

Count duplicate values in a sequence

How can I count duplicate data in a column using excel?
Example
A B
apple 1
apple 2
apple 3
ball 1
bat 1
dog 1
dog 2
gole 1
gole 2
gole 3
gole 4
I think there is no uniform formula for all cells in your case.
I would suggest something like this:
Put this on B column and then fill down the rest.
=COUNTIF($A$1:$A1,$A1)
After you fill the other rows, values of rows would look like this
B1=COUNTIF($A$1:$A1,$A1)
B2=COUNTIF($A$1:$A2,$A2)
B3=COUNTIF($A$1:$A3,$A3)

Resources