How do I count specific values across multiple columns in pandas

2024/9/16 23:07:23

I have the DataFrame

df = pd.DataFrame({'colA':['?',2,3,4,'?'],'colB':[1,2,'?',3,4],'colC':['?',2,3,4,5]
})

I would like to get the count the the number of '?' in each column and return the following output -

colA - 2
colB - 1
colC - 1

Is there a way to return this output at once. Right now the only way I know how to do it is write a for loop for each column.

Answer

looks like the simple way is

df[df == '?'].count()

the result is

colA    2
colB    1
colC    1
dtype: int64

where df[df == '?'] give us DataFrame with ? and Nan

  colA colB colC
0    ?  NaN    ?
1  NaN  NaN  NaN
2  NaN    ?  NaN
3  NaN  NaN  NaN
4    ?  NaN  NaN

and the count non-NA cells for each column.

Please, look on the other solutions: good readable and the most faster

https://en.xdnf.cn/q/72559.html

Related Q&A

Split Python source into separate directories?

Here are some various Python packages my company "foo.com" uses:com.foo.bar.web com.foo.bar.lib com.foo.zig.web com.foo.zig.lib com.foo.zig.lib.lib1 com.foo.zig.lib.lib2Heres the traditional …

How can I use a raw_input with twisted?

I am aware that raw_input cannot be used in twisted. However here is my desired application.I have an piece of hardware that provides an interactive terminal serial port. I am trying to connect to th…

How to use Python and HTML to build a desktop software?

Maybe my question is stupid but I still want to ask. I am always wondering whether I can use Python, HTML and Css to develop a desktop software. I know there are alrealy several good GUI frameworks lik…

More efficient way to look up dictionary values whose keys start with same prefix

I have a dictionary whose keys come in sets that share the same prefix, like this:d = { "key1":"valA", "key123":"valB", "key1XY":"valC","…

When should I use dt.column vs dt[column] pandas?

I was doing some calculations and row manipulations and realised that for some tasks such as mathematical operations they both worked e.g.d[c3] = d.c1 / d. c2 d[c3] = d[c1] / d[c2]I was wondering wheth…

Quiver matplotlib : arrow with the same sizes

Im trying to do a plot with quiver but I would like the arrows to all have the same size.I use the following input :q = ax0.quiver(x, y, dx, dy, units=xy ,scale=1) But even if add options like norm = t…

How to convert Tensorflow dataset to 2D numpy array

I have a TensorFlow dataset which contains nearly 15000 multicolored images with 168*84 resolution and a label for each image. Its type and shape are like this: < ConcatenateDataset shapes: ((168, 8…

CSV remove field value wrap quotes

Im attempting to write a list to a csv, however when I do so I get wrapper quotes around my field values:number1,number2 "1234,2345" "1235.7890" "2345.5687"Using this code…

Python - Py_Initialize unresolved during compilation

I have statically compiled Python2.7 without any error. To test my build, I use the following snippet: #include "Python.h" int main() {Py_Initialize(); }And I am compiling it like this:$ gcc…

Python download large csv file from a url line by line for only 10 entries

I have a large csv file of the client and shared via a url to download and I want to download it line by line or by bytes and I want to limit only for 10 entries.I have the following code which will do…