简体   繁体   中英

How to apply fillna symetrically on a pd.dataframe?

I'm automatically extracting tabular Data from PDFs with Camelot. Quite often the edge detection leads towards automatically aligning text in a middle cell.

Hence I need to process the extracted pandas-dataframe further. I want to spread the text that is aligned in the middle to over the other cells, strictly speaking copying it symmetrically.

So if both neighbour cells have the "value" np.nan both get the copied value, if on both sides two cells have np.nan all four of them get the value of the middle cell and so on.

This is an example for a current dataframe.

Column_1     Column_2    Column_3  Column_4 

5            np.nan           6          np.nan
np.nan       some Text        np.nan       12
7           some other Text     8          np.nan

The expected result is should be this:

 Column_1    Column_2    Column_3  Column_4 

     5        np.nan         6       12
some Text   some Text    some Text   12
     7     some other Text   8       12

To fill the text directly to replace np.nan, you can use this option:

df['column_name'] = np.where(df['column_name'].isnull(), 'text', df['column_name'])

To fill the values based on a particular column's previous row or next row value:

df.fillna( method ='bfill/ffill', inplace = 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