Excel formulas matching numbers [duplicate] - excel

I have been looking through all different sources and cannot find the exact answer to this. Was hoping someone can help me out.
I have two columns:
COL1 COL2
abc defghe
def iabclmn
ghi zhued
fgh lmnop
I want to know if a value in COL1 exist in COL2. So in this case I want it to look like this:
COL1 COL2 COL3
abc defghe TRUE
def iabclmn TRUE
ghi zhued FALSE
fgh lmnop TRUE
Is there a function that can do this, I have over 500 rows so I cannot just call out specific values?
I know there is an example that does specific values like this, but I want it to be by the entire column:
=ISNUMBER(SEARCH(substring,text))
Thanks!

To do it for full columns as real non-array formula:
=COUNTIF(B:B,"*"&A1&"*")>0

This will do it:
=SUMPRODUCT(ISNUMBER(SEARCH(A1,$B$1:$B$4))*1)>0
The SUMPRODUCT() forces it to iterate through Column B and keep track of the ones that return true. So if any are found it adds 1 to the pool.
The >0 test whether any returned TRUE.

Related

subtracting values between columns both ways

I need to find those values that are in column 1 and not in column 2 and vica versa. It can look like this: take fist row in the first column and look if there is same number in the second column if so then on the third column write 0 (substraction) and if there won't be the same number then write searched number or error, doesn't matter. This should work both ways (some numbers can be in col2 but not in col1, those i need to find aswell). So probably there would be 2 formulas in 2 columns. one searching from col1 to col2, and same for col2 to col1. And if there for example in col1 would be twice some value and in col2 just once, than it should show for the first number 0 and for second number error or searched number.
Dataset looks like this:
Col1
Col2.
42646
55
42646
77
33
25
77
Col3
Col4
0
55
0
0
33(or error,NA etc)
25
0
I have tried vlook up, but wasn't sucesfull.
I guess this is what you are looking for. You can use for Col3:
=IF(A2:A6="", "",IF(ISNA(XMATCH(A2:A6,B2:B6)),A2:A6,0))
and for Col4:
=IF(B2:B6="", "", IF(ISNA(XMATCH(B2:B6,A2:A6)),B2:B6,0))
Both formulas returns 0 if the value was found (including blanks), otherwise the missing value.
You can put all together using HSTACK:
= HSTACK(IF(A2:A6="", "",IF(ISNA(XMATCH(A2:A6,B2:B6)),A2:A6,0)),
IF(B2:B6="", "", IF(ISNA(XMATCH(B2:B6,A2:A6)),B2:B6,0)))
Or using LET to avoid repetitions.
= LET(A, A2:A6, B, B2:B6, HSTACK(IF(A="","",IF(ISNA(XMATCH(A,B)),A,0)),
IF(B="", "", IF(ISNA(XMATCH(B,A)),B,0))))
Here is the output:
You can use XLOOKUP too, but the formula is longer, because the first three input arguments are required:
=IF(ISNA(XLOOKUP(A2:A6,B2:B6, A2:A6)),A2:A6,0)
A shame you haven't added a sample in your data that would show what you meant with:
"And if there for example in col1 would be twice some value and in col2 just once, than it should show for the first number 0 and for second number error or searched number."
Your requirements make this a little tricky, but try:
Formula in C1:
=IF(A1="","",IF(COUNTIF(B:B,A1)-COUNTIF(A$1:A1,A1)<0,A1,0))
Formula in D1:
=IF(B1="","",IF(COUNTIF(A:A,B1)-COUNTIF(B$1:B1,B1)<0,B1,0))

MS excel calculate sum by conditions

I have a table:
col1
col2
134
1
432
2
222
3
21
4
982
5
1352
8
111
9
I need to find all possible sum combinations of col1 values IF col2 sum is 10. (5+4+1, 2+3+5, etc.) & number of terms is 3
Please advice how to solve this task?
To get all unique possible sums based on a give count of items in col2 and sum of col2 is a specific amount, with ms365, try:
Formula in D1:
=LET(inp,B1:B7,cnt,3,sm,10,B,COUNTA(inp),A,MAKEARRAY(B,cnt,LAMBDA(r,c,INDEX(inp,r,1))),D,B^cnt,E,UNIQUE(MAKEARRAY(D,cnt,LAMBDA(rw,cl,INDEX(IF(A="","",A),MOD(CEILING(rw/(D/(B^cl)),1)-1,B)+1,cl)))),F,FILTER(E,MMULT(--(E<>""),SEQUENCE(cnt,,,0))=cnt),G,FILTER(F,BYROW(F,LAMBDA(a,(SUM(a)=sm)*(COUNT(UNIQUE(a,1))=cnt)))),UNIQUE(BYROW(G,LAMBDA(a,SUM(XLOOKUP(a,inp7,A1:A7))))))
You can now change parameters cnt and sm to whichever amount you like.
The trick is borrowed from my answer here to calculate all permutations first. Instead of a range, the single column input is extended using MAKEARRAY().
A short visual representation of what is happening:
Expand the given array based on cnt;
Create a complete list of all possible permutations;
Filter step 2 based on a sum per row and unique numbers (don't use values from col2 more than once);
Lookup each value per row to create a total sum per row;
Return only the unique summations as per screenshot at the top.

Fill columns values according to a match between the columns names and the items of a list in another column

I would like to fill columns values with a "yes" or "no" based on a match between the column name or its substrings and the items of a list in the same row but in another column. Is there a way to achieve this using pandas ?
Out[5]:
Insurance:retailers Insurance:buyers Insurance:sales Types
0 [retailers, sales]
1 [sales]
2 [retailers, buyers]
I'm trying to achieve the following result:
Out[7]:
Insurance:retailers Insurance:buyers Insurance:sales Types
0 yes no yes [retailers, sales]
1 no no yes [sales]
2 yes yes no [retailers, buyers]
Any help would be much appreciated. Thank you.
Don't know much about pandas, but from my understanding panda's DataFrames can be constructed from a python dictionary and vice versa.
So this should point you in the right direction
data={'Insurance:retailers':['No','No','No'],
'Insurance:buyers':['No','No','No'],
'Insurance:sales':['No','No','No'],
'Types':[['retailers', 'sales'],['sales'],['retailers', 'buyers']]}
for idx, entry in enumerate(data['Types']):
for key, value in data.items():
if any(element in key for element in entry):
data[key][idx]='yes'
print(data)

How to snip part of a rows' data and only leave the first 3 digits in Python

0 546/001441
1 540/001495
2 544/000796
3 544/000797
4 544/000798
I have a column in my dataframe that I've provided above. It can have any number of rows depending on the data being crunched. It is one of many columns but the first three numbers match another columns data. I need to cut off everything after the first 3 numbers in order to append it to another dataframe based off of the similar values. Any ideas as to how to get only the first 3 numbers and cut off the remaining 8 values?
So far I've got the whole column singled out as a Series and also as a numpy.array in order to try to convert it to a str instead of an object.
I know this is getting me closer to an answer but i can't seem to figure out how to cut out the unnecessary values
testcut=dfwhynot[0][:3]
this cuts the string where i need it, but how do i do this for the whole column is what i can't figure out.
Assuming the name of your column is col, you can
# Split the column into two
df['col'] = df['col'].apply(lambda row: row.split('/'))
df[['col1', 'col2']] = pd.DataFrame(df_out.values.tolist())
col1 col2
0 546 001441
1 540 001495
2 544 000796
3 544 000797
4 544 000798
then get the minimal element of each col1 group
df.groupby('col1').min().reset_index()
resulting in
col1 col2
0 540 001495
1 544 000796
2 546 001441

Item in String - Create new column

So I have found some other answers to this question that involved conditionally selecting with operators but I haven't found a solution that involves contain statements.
What I am trying to accomplish is inside a dataframe
A cat ?
B dog ?
C rat ?
How do I set the third column to a value depending on whether the second column contains 'a'?
Use contains:
df['Col2'] = df['Col1'].str.contains('a')
Output:
Col0 Col1 Col2
0 A cat True
1 B dog False
2 C rat True

Resources