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)
python pandas
Sample code has been created from answers and comments on other Q&A sites.
The key points are as follows.
pd.to_numeric
numbers to NaN
non-numerical valuesdf.dropna()
allows you to delete a line with NaN
`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)
© 2024 OneMinuteCode. All rights reserved.