Excel find string in string in range return data in corresponding column - excel

The Excel thingy again. ;-)
I have columns like this:
A B C
UserID Name Org_Name
1 Brian Green Susan Red
2 Niels Red Susan Blue
3 Susan Yellow Brian Green
4 India Orange Serge Black
I am looking for a formula that can find ORG_Name(C) in Name(B) and return the UserID(A) and Name(B) found.
In this case it could look like this:
A B C D E
UserID Name Org_Name FoundID FoundName
1 Brian Green Susan Red N/A N/A
2 Niels Red Susan Blue N/A N/A
3 Susan Yellow Brian Green 1 Brian Green
4 India Orange Serge Black N/A N/A
Anyone?

Formula for FoundID column:
=INDEX($A$2:$A$5,MATCH(C2,$B$2:$B$5,0))
Formula for FoundName column:
=INDEX($B$2:$B$5,MATCH(C2,$B$2:$B$5,0))
Adjust the end row as required (the 5s in both formulas)

Related

Join on a second column if there is not a match on the first column of a pandas dataframe

I need to be able to match on a second column if there is not a match on the first column of a pandas dataframe (Python 3.x).
Ex.
table_df = pd.DataFrame ( {
'Name': ['James','Tim','John','Emily'],
'NickName': ['Jamie','','','Em'],
'Colour': ['Blue','Black','Red','Purple']
})
lookup_df = pd.DataFrame ( {
'Name': ['Tim','John','Em','Jamie'],
'Pet': ['Cat','Dog','Fox','Dog']
})
table_df
Name NickName Colour
0 James Jamie Blue
1 Tim Black
2 John Red
3 Emily Em Purple
lookup_df
Name Pet
0 Tim Cat
1 John Dog
2 Em Fox
3 Jamie Dog
The result I need:
Name NickName Colour Pet
0 James Jamie Blue Dog
1 Tim Black Cat
2 John Red Dog
3 Emily Em Purple Fox
which is matching on the Name column, and if there is no match, match on the Nickname column,
I tried many different things, including:
pd.merge(table_df,lookup_df, how='left', left_on='Name', right_on='Name')
if Nan -> pd.merge(table_df,lookup_df, how='left', left_on='NickName', right_on='Name')
but it does not do what I need and I want to avoid having a nested loop.
Has anyone an idea on how to do this? Any feedback is really appreciated.
Thanks!
You can map on Name and fillna on NickName:
s = lookup_df.set_index("Name")["Pet"]
table_df["pet"] = table_df["Name"].map(s).fillna(table_df["NickName"].map(s))
print (table_df)
Name NickName Colour pet
0 James Jamie Blue Dog
1 Tim Black Cat
2 John Red Dog
3 Emily Em Purple Fox

Averageifs with Or

I have read that this may not be possible/ I am just doing it wrong so I was wondering if anyone knew a workaround (non - VBA) or could correct me? Thanks!
Col A has the price of apples and oranges
Col D has apples and Oranges
Col E has the type of fruit (Red, Yellow, Green, Purple)
I'm trying to get the average of Col A where Col D is Apples and Col E is either Red or Yellow.
I tried:
=SUM(AVERAGEIFS(A:A,D:D,"Apples",E:E,"Red"),AVERAGEIFS(A:A,D:D,"Apples",E:E,"Yellow"))/2
But it did not work - anyone have any ideas?
Yes you can do this:
=SUM(SUMIFS(A:A,D:D,"Apples",E:E,{"Red","Yellow"}))/SUM(COUNTIFS(D:D,"Apples",E:E,{"Red","Yellow"}))
Confirm with Enter
Edit Comparison of formulas
1) In this first screenshot all results appear similar:
2) In this second screenshot I changed cell E8 to purple. Results start to differ:
3) In this third screenshot it becomes clear there is no result using AVERAGE while there is using SUM/COUNTIFS:
I don't think you can add OR operations in AVERAGEIFS but you can use this (it's an array formula so you'll have to enter it with ctrl + shift + enter):
=AVERAGE(IF((D:D="Apples")*((E:E="Yellow")+ (E:E="Red")),A:A))
If you don't want to use array formula then (as an alternative to #JvdV's answer), you can use:
=SUMPRODUCT(A:A,--(D:D="Apples"), --((E:E="Red")+(E:E="Yellow")))/SUMPRODUCT(--(D:D="Apples"), --((E:E="Red")+(E:E="Yellow")))
Similar to the response from JvdV, you can use AVERAGEIFS directly.
=AVERAGE(AVERAGEIFS(A:A, B:B, {"a","c"}))
'for your data
=AVERAGE(AVERAGEIFS(A:A,D:D, "Apples", E:E, {"Red","Yellow"}))
Addendum:
4.00 apples Red
2.70 oranges Purple
2.13 apples Purple
2.64 apples Yellow
3.11 apples Purple
2.96 apples Purple
9.44 apples Yellow
2.07 oranges Red
3.14 oranges Purple
3.58 oranges Yellow
2.44 oranges Red
2.82 apples Red
2.86 apples Yellow
2.23 oranges Yellow
1.95 apples Red
2.37 oranges Yellow
The above sample data produces identical results for the following:
=AVERAGE(A2,A5,A8,A13,A14,A16)
=AVERAGE(AVERAGEIFS(A:A,D:D,"apples",E:E,{"red","yellow"}))
=SUM(SUMIFS(A:A, D:D, "apples", E:E, {"red","yellow"}))/SUM(COUNTIFS(D:D, "apples", E:E, {"red","yellow"}))
However when there are a different number of matches in the { OR } criteria then there are bad results.
4.00 apples Red
2.70 oranges Purple
2.13 apples Purple
2.64 apples Yellow
3.11 apples Purple
2.96 apples Purple
9.44 apples Yellow
2.07 apples Red
3.14 oranges Purple
3.58 oranges Yellow
2.44 oranges Red
2.82 apples Red
2.86 apples Yellow
2.23 oranges Yellow
1.95 apples Red
2.37 oranges Yellow

counting unique row values in excel

I have product list like this. I have only product name list
Apple 1
Apple 1
Apple 1
Orange 2
Orange 2
Orange 2
Mango 3
Mango 3
Pineapple 4
Pineapple 4
Pineapple 4
Pineapple 4
Pineapple 4
Pineapple 4
Avocado 5
I want to count data in this way. please help.
In B1 insert 1 and in B2 insert:
=IF(A2=A1,B1,B1+1)
And from there just fill.
Put 1 in B1 then put this formula in B2,
=if(a2<>a1, max(b$1:b1)+1, b1)
... and fill down.
Alternately just put his formula into B1,
=SUMPRODUCT(1/(COUNTIF(A$1:A1,A$1:A1)))
... and fill down.

Transposing Sections of Data in Excel VBA

I need some help to 'transpose' several blocks of data to another area of the worksheet on some sort of loop. The data is currently set out as per below:
Rob Dave John Peter Jane Lily Mel Amy
1 4 7 3 2 5 8 6
blue green brown black purple orange yellow white
apple pear grape lemon banana kiwi mango strawberry
I was wondering if anyone might know some relevant VBA code that might let me transform it into the following:
Rob Dave John Peter
1 4 7 3
blue green brown black
apple pear grape lemon
Jane Lily Mel Amy
2 5 8 6
purple orange yellow white
banana kiwi mango strawberry
This is just two groups of horizontally arranged data that I want to shift into vertical blocks. The actual workbook contains many more blocks of data so may require some sort of loop. Thanks in advance.
Set the variable nBlocks to the total number of 4 X 4 blocks you have and run:
Sub BlockMover()
Dim nBlocks As Long, N As Long, i As Long
Dim r1 As Range, r2 As Range
nBlocks = 3
For i = 2 To nBlocks
N = Cells(Rows.Count, "A").End(xlUp).Row + 1
Set r1 = Range(Cells(1, 4 * (i - 1) + 1), Cells(4, 4 * (i - 1) + 4))
Set r2 = Range("A" & N)
r1.Copy r2
r1.Clear
Next i
End Sub
I would use the built in function Transpose().

Get Top Performer by Subgroup Using Index and Match

I am trying to rank names in Column C from largest to smallest score.
Category Score Name Total Rank Apple Rank Orange Rank
Apple 10 Joe Rachel Rachel 0
Orange 15 Don Natalie 0 Natalie
Apple 20 James Tom Tom 0
Apple 1 Rob Nothing Nothing 0
Orange 3 Mary Gina 0 Gina
Orange 100 Rachel James 0 James
Orange 99 Natalie Don 0 Don
Orange 87 Tom Joe 0 Joe
Apple 27 Gina Mary Mary 0
Orange 30 Nothing Rob 0 Rob
This works in Column E for Apples AND Oranges, with formula in E2 that is
=INDEX($C$2:$C$25,MATCH(1,INDEX(($B$2:$B$25=LARGE($B$2:$B$25,ROWS(E$1:E1)))*(COUNTIF(E$1:E1,$C$2:$C$25)=0),),0))
However, the goal is to compare Apples to Apples and Oranges to Oranges.
Only, the formulas in Columns F and G show "0" values for those rows that aren't in the right Apple/Orange category.
For F2:
=IF($A:$A="Apple",INDEX($C:$C,MATCH(1,INDEX(($B:$B=LARGE($B:$B,ROWS(F$1:F1)))*(COUNTIF(F$1:F1,$C:$C)=0),),0)),0)
For G2:
=IF($A:$A="Orange",INDEX($C:$C,MATCH(1,INDEX(($B:$B=LARGE($B:$B,ROWS(G$1:G1)))*(COUNTIF(G$1:G1,$C:$C)=0),),0)),0)
How do I modify the codes so that 0 values won't show up?
Something like this would be great: (screenshot made by just copy pasting values...)
Apple Rank Orange Rank
Rachel Natalie
Tom Gina
Nothing James
Mary Don
Joe
Rob
Note: Unless the whole column ranges are required the steps below may seem to take an uncomfortably long time if these ranges are not restricted.
Assuming you have what below is in ColumnA:G and a corresponding layout:
then ColumnsI:J may be achieved quite simply by copying ColumnF:G and Paste Special..., Values into I1, then select ColumnsI:J, HOME > Editing - Find & Select, Replace..., Find what: 0, Replace with: , Replace All followed by Find & Select, Go To Special..., select Blanks (only), OK, right-click on one of the chosen cells and Delete..., Shift cells up, OK.
To remove the 0s from ColumnF:G only replacing the final 0 in each formula with "" is sufficient.

Resources