Python error: TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

2024/10/13 22:24:57

Below is a small sample of my dataframe.

In [10]: dfOut[10]:TXN_KEY Send_Agent  Pay_Agent  Send_Amount  Pay_Amount
0     13272184  AWD120279  AEU002152        85.99       85.04
1     13272947  ARA030122  AEV000034       402.01      400.00
2     13273453  API185805  API190139        22.98       22.98
3     13273870  ANO080012  API352676       490.00      475.68
4     13274676  AUK359401  AED002782       616.16      600.87
5     13274871  ACL000105  ACB020203       193.78      185.21
6     13275194  ADA374245  ANP023424       100.00       98.50grouped = df.groupby('Send_Agent')

A function to calculate the number of times the Send_Amount is same as the next one works just fine.

   def repeat_count(s):return (s.shift() == s).sum()In [35]: result = grouped.agg({'Send_Amount':[repeat_count]})In [36]: resultOut[36]:Send_Amountrepeat_count
Send_Agent
A03010016             0
A03020026             0
A10000075             0
A10000316             0
A10000932             0
A10060013             0
A10080022             0
A11120030             0

But when I am trying to calculate the count of all the Send_Agent where Send_Amount is between 900 and 1000 and I am getting an error:

In [27]: def banded_txns(s):
return(s >=900.00 & s  < 1000.00).count()In [28]: result = grouped.agg({'Send_Amount':banded_txns})TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

I am not able to figure out why it is working for one type of comparison and not for the other. Thanks in advance.

Answer

You're missing some parentheses:

def banded_txns(s):return ((s >= 900.00) & (s < 1000.00)).count()
https://en.xdnf.cn/q/118028.html

Related Q&A

How to remove grey boundary lines in a map when plotting a netcdf using imshow in matplotlib?

Is it possible to remove the grey boundary lines around the in following map? I am trying to plotting a netcdf using matplotlib.from netCDF4 import Dataset # clarify use of Dataset import matplotlib.p…

function that takes one column value and returns another column value

Apologies, if this is a duplicate please let me know, Ill gladly delete.My dataset: Index Col 1 Col 20 1 4, 5, 6 1 2 7, 8, 9 2 3 10, 11, 12 3 …

Python write value from dictionary based on match in range of columns

From my df showing employees with multiple levels of managers (see prior question here), I want to map rows to a department ID, based on a manager ID that may appear across multiple columns:eid, mid…

Merge two dataframes based on a column

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ? df1place name qty unit…

Python/Pandas - building a new column based in columns comparison

I have this dataframe:df:CNPJ Revenues 2016 Revenues 2015 Revenues 2014 0 01.637.895/0001-32 R$ 12.696.658 NaN R$ 10.848.213 1 02.916.265/0001-60 …

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…

Images appearing in all but 1 flask page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I cant get any images to load using already working code…

Python: Write nested list objects to csv file

Im trying to write data from a list of lists to a csv file. This is a simplified version of what I haveclass Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightde…

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like thisRoot--Network--DTOLots of py files which contain classes.Other py files in the projectBoth in the Network and the DTO folder there is an…

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…