I want to delete non-numeric (character, blank line, etc.) lines from the Excel file.

Asked 2 years ago, Updated 2 years ago, 44 views

I would like to delete non-numeric (letter, blank line, etc.) lines in the Excel file in Python.
What should I do?

I managed to delete the blank line with the code below, but
It's hard to find a way to delete a line that contains characters, symbols, etc. on the Internet.

I would appreciate it if you could give me guidance.
Thank you for your cooperation.

Remove blank lines

df=pd.read_excel('target+notarget_unknown.xlsx', sheet_name='description variable')
df2 = df.dropna()

Excel data (I want to delete the entire line (2nd to 6th lines) containing the blue part (blanks and letters)

Excel

python pandas

2022-09-29 22:01

1 Answers

Sample code has been created from answers and comments on other Q&A sites.
The key points are as follows.

  • Convert Series values to pd.to_numeric numbers to NaN non-numerical values
  • df.dropna() allows you to delete a line with NaN `
  • To exclude the header column, slice it with df.columns[1:] or specify it with the index_col argument in pd.read_excel
import pandas as pd
import dataframe as df

df=pd.read_excel('Book1.xlsx', sheet_name='Description Variable', index_col=0)
for iindf.columns:
    df[i]=pd.to_numeric(df[i], errors='coerce')
df = df.dropna()
print(df)


2022-09-29 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.