for given data i need to find the count of "a" in the column Key - python-3.x

Key
----------
0 a
1 a
2 b
3 b
4 a
5 c
so far i tried this:
df.groupby(["key1"],).count()
However it is also showing the counts of b and c, i want only for a.

Create mask and count by sum:
df["Key"].eq('a').sum()

Related

Excel function using value from previous function not working

I'm trying to use the following max function by the group in column A. The value I want in C is the max entry of each group in column B. When column B is normal numbers, this function works. However, I would like column B to be populated by the results of a different function (in this case an =IF function that successfully returns a number). When I do this, I get 0's.
in column C: =MAX(IF($A$2:$A$5=C2,$B$2:$B$5))
Column A
Column B
Column C
Group 1
5
5
Group 1
4
5
Group 2
6
7
Group 2
7
7
Column A
Column B
Column C
Group 1
=IF(...)
0
Group 1
=IF(...)
0
Group 2
=IF(...)
0
Group 2
=IF(...)
0
Any idea what could be going on? Please let me know if I can provide anything additional to help explain.
Thank you!
Reese

Filtering two columns: keep all the rows associated to one ID if exists a value in the second column

I have a table with various columns but i need to filter it based on two, the table structure is the following:
ID Test
1 A
1 B
1 C
2 B
2 D
3 A
4 A
4 C
4 D
5 B
5 C
What i need to do is keeping all rows associated to one ID if exists the case where the test is "A", the filtered table should then be:
ID Test
1 A
1 B
1 C
3 A
4 A
4 C
4 D
Is there a way to do this?
Alternatively you can use:
=FILTER(A1:B11,COUNTIFS(A1:A11,A1:A11,B1:B11,"A"))
Or, based on your comment:
=LET(X,COUNTIFS(B1:B11,B1:B11,L1:L11,"A"),INDEX(FILTER(B1:L11,X),SEQUENCE(SUM(X)),{1;11}))
If you have Excel365 and access to dynamic formulas then try below-
=FILTER(A1:B11,ISNUMBER(MATCH(A1:A11,UNIQUE(FILTER(A1:A11,B1:B11="A")),0)))

How can I count the number of values by group in excel

I was wondering if there was a way to count the number of values by category. Example:
A 3
A 3
A 3
B 4
B 4
B 4
B 4
C 5
C 5
C 5
C 5
C 5
D 2
D 2
What is happening there is that there are 5 categories "A, B, C, D" and there are different counts of it. Duplicate values. I would like to create a new column and output the number of times it occurs in a different column as shown above. Please no VBA as i don't know it.
Try this...
=IF(A2<>A1,COUNTIF(A:A,A2),"")

Excel Formula comparing two columns

Below is a sample of the data I have. I want to match the data in Column A and B. If column B is not matching column A, I want to add a row and copy the data from Column A to B. For example, "4" is missing in column B, so I want to add a space and add "4" to column B so it will match column A. I have a large set of data, so I am trying to find a different way instead of checking for duplicate values in the two columns and manually adding one row at a time. Thanks!
A B C D
3 3 Y B
4 5 G B
5 6 B G
6 8 P G
7 9 Y P
8 11 G Y
9 12 B Y
10
11
12
11
12
I would move col B,C,D to a separate columns, say E,F,G, then using index matches against col A and col B identify which records are missing.
For col C: =IFERROR(INDEX(F:F,Match(A1,E:E,0)),"N/A")
For col D: =IFERROR(INDEX(G:G,Match(A1,E:E,0)),"N/A")
Following this you can filter for C="N/A" to identify cases where a B value is missing for an A value, and manually edit. Since you want A & B to be matching here col B is unnecessary, final result w/ removing col B and C->B, D->C:
A B C
3 Y B
4 N/A N/A
5 G B
6 B G
7 N/A N/A
Hope this helps!

Excel vlook up not blank multi values

I would like to get the first value (for a given key)from the vlookup that is not blank.
1 A 1 A
2 2 C
2 C => 3 B
3 B 4 W
4 W
4 X
Is it possible with vlookup or do I have to use INDEX, MATCH, CHOOSE etc?
If so can anyone provide an example? I cannot add extra columns.
Try this:
=IF(VLOOKUP($D1;$A$1:$B$6;2;FALSE)=0;VLOOKUP($D1;$A$1:$B$6;2;TRUE);VLOOKUP($D1;$A$1:$B$6;2;FALSE))
Use this formula, it looks for the first non blank:
=INDEX($B$1:$B$6,AGGREGATE(15,6,ROW($B$1:$B$6)/(($A$1:$A$6=D1)*($B$1:$B$6<>"")),1))
You should use a IF in an intermediate column, then use this intermediate in your VLOOKUP formula:
This gives, using an extra_tab if you can't insert columns in current sheet
Sheet1 extra_tab
A C D E A B
------------- ---------
1 A 1 A 1 A
2 2 C 0
2 C 3 B 2 C
3 B 4 W 3 B
4 W 4 W
4 Z 4 Z
To avoid blanks for further calculation Formula in extra_tab.A1 and extra_tab.B1 is:
A B
=Sheet1!B1 =IF(Sheet1!B1="";"";Sheet1!A1)
Formula in sheet1.D1 is:
==VLOOKUP(C1;extra_tab!A:B;2;FALSE)
Hope it helps

Resources