-1

I am trying do delete or replace all cells that aren't numbers in a column.

I used the code below:

if  file[[Coluna_Vaos]] is not float() or int():
ColVao = file[[Coluna_Vaos]].replace(**"-" or "!"**, np.NaN) 

But I have to show every type that I don't want. If exist a variable for that that contain all things except numbers would be useful.

Resuming: I want to remove everything that is not a number on that row. I would appreciate if somebody could let the solution for other example, like check and rip if that is another thing besides strings.

BigBen
  • 46,229
  • 7
  • 24
  • 40

1 Answers1

0

You can use pd.to_numeric with errors='coerce' and then replace the NaN values or drop those rows.

df['newcol'] = pd.to_numeric(df['Coluna_Vaos'], errors='coerce')
df = df.dropna(subset=['Coluna_Vaos'])

# or
df['newcol'] = df['newcol'].fillna(0)
Eric Truett
  • 2,970
  • 1
  • 16
  • 21
  • file['Vãos'] = pd.to_numeric(file["I"], errors = 'coerce') print(file['Vãos']) Gives: TypeError: only integer scalar arrays can be converted to a scalar index – Lucas Francklin Apr 18 '21 at 22:59