datascana
datascana

Reputation: 641

invalid type comparison error

I have been using this code:

df = df[df['A']>0]
df.loc[(df['A']<0), 'A'] = df['A'].median()

I am getting error invalid type comparison.

I would like to replace all values in the column A that are negative with median, mean or drop them.

Any explanation ?

EDIT: (continuation of Calculate column value based on 2 dataframes)

df1['A'] = dates.sub(df1['Date1'], axis=0)
print (df1)

          Date1     A
L-22 2015-03-12   668
L-15 2016-02-26   -46  

Upvotes: 2

Views: 4081

Answers (1)

jezrael
jezrael

Reputation: 862441

There is problem some bad nonumeric value, so need to_numeric:

df['A'] = pd.to_numeric(df['A'], errors='coerce')

For filtering need boolean indexing:

df = df[df['A'] < 0]

Upvotes: 3

Related Questions