Python - Pandas - Dataframe How to add variables to a column when using .count

Viewed 20

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

1 Answers

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()
Related