Reputation: 77
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?
Upvotes: 0
Views: 719
Reputation: 71
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
Upvotes: 1