简体   繁体   中英

Python Pandas remove white blank space after import data from excel

I import some data from excel to dataframe. In excel there are some cells which are blank (not empty) ie someone has pressed spacebar from keyboard to fill that cell in excel, therefore no characters but still looks blank. In dataframe i tried to clean it up with below function. But dataframe doesn't show as NAN after cleaning. Is there a function available so that it can be cleaned?

df.columns = df.columns.str.strip()

I can't reply to your comment because I have no rep:(.

If I am understanding you correctly, you wish to place a NaN value where there are spaces?

I tried the following and it seems to work, let me know if this helps.

import pandas as pd
import numpy as np

df = pd.DataFrame({'Names': ['betty', 'chris',' ',  'steve', 'carly']})

df.loc[df['Names'] == ' '] = np.nan

If you need to iterate over each column you can put the df.loc within a loop like the following.

df = pd.DataFrame({'Names': ['betty', 'chris',' ',  'steve', 'carly'],'Age':\
               ['40', ' ', '32', '44', '69']})

for col in df.columns:
    df[col].loc[df[col] == ' '] = np.nan

Are you sure df.columns = df.columns.str.strip() is what you want? That only changes the column names . If you want to change the values inside the cells, consider replace :

df.replace('^\s+$', np.nan, regex=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM