I am creating a new column that will count all ids but exclude a few rows of customers where the customer's ID has a certain prefix and has no repeat orders
df['newcolumn'] = df[(df.notnull['Date']) & (df['ID'].str.contains('prefix')) & (df['Repeat order'] == 'No')].groupby(['ID'], as_index=False).count()
I am getting below error
TypeError: 'method' object is not subscriptable
Still no difference when I replaced [] with () to resolve above error
I also replaced df[(df['Date'].notnull)
and got TypeError: unsupported operand type(s) for &: 'method' and 'bool' error
This is the coding equivalence of run-on sentence during English class. You were trying to stuff so many things into a single line that it's hard to tell where the problem is.
Break up you code:
cond = (
df['Date'].notnull()
& df['ID'].str.contains('prefix')
& df['Repeat order'].eq('No')
)
df['newcolumn'] = df[cond].groupby(['ID'], as_index=False).count()
NB: your original error was because of this:
df.notnull['Date']
df.notnull
is a method. It's not subscriptable. You can execute the method and subscript the result:
df.notnull()['Date']
or you can extract a column from the data frame and run notnull
on it:
df['Date'].notnull()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.