0

I have a excel file, has file name and a value, for example:

file.   count
001.txt    1
002.txt    2
003.txt    2
004.txt    3
005.txt    1
006.txt    2

I'm using the following code to find how many 2s are in the value column, but somehow the result is 0

df = pd.read_excel('report.xlsx')
df.columns = ['file', 'count']
count = df['count'].tolist().count('2')
print(count)

>>0

Did I do something wrong in the code?

Chang
  • 77
  • 6
  • The shown column is almost certainly read in as a _number_ not a string.`df['count'].tolist().count(2)` However converting to a list and using list count is very expensive, it should almost certainly be `count = df['count'].eq(2).sum()` as outlined here [Python Pandas Counting the Occurrences of a Specific value](https://stackoverflow.com/q/35277075/15497888) – Henry Ecker Sep 21 '21 at 22:23

1 Answers1

1

Firstly, check the column 'count' if it's 'number'(such as 'int64','int32','float64',etc) or 'object'.

df['count']    # check the result

If the data type is 'number', then you can use the code you worte, but just correct like this:

df['count'].to_list().count(2)

I guess when you .to_list, the elements are all numbers. And count('2') means to count the string element — '2' how many times it appears, while there was no elements like '2'. That's why you got zero result.

Here is a simple example.

lis = [0, 1, 2, 2, 3]
lis.count('2')   # it returns 0
lis.count(2)     # it returns 2
kevinskey
  • 71
  • 3