I have an Excel file which contains the following structure with no header or index - I've added just the column header from Excel without existing in my table:
A | B | C | D | E | ... | J | K |
---|---|---|---|---|---|---|---|
ABC | XLS | 1231341231 | 123123asdad1923 | NaN | NaN | ... | 123123asdad1923 |
DEF | XLS | 1231231231 | 1231823asda9123 | NaN | askda213 | ... | 123123asdad1923 |
XYZ | XLS | 1231231233 | 2138820394832sd | NaN | NaN | ... | asdasdq2ew12332 |
I would need that all the cells that contains NaN
to be removed and while NaN, move the cells to the left.
Output should look like:
A | B | C | D | E | ... | J | K |
---|---|---|---|---|---|---|---|
ABC | XLS | 1231341231 | 123123asdad1923 | 123123asdad1923 | |||
DEF | XLS | 1231231231 | 1231823asda9123 | askda213 | ... | 123123asdad1923 | |
XYZ | XLS | 1231231233 | 2138820394832sd | asdasdq2ew12332 |
I have found this similar question dropped on Stackoverflow, but the answer is not doing any change:
import pandas as pd
df = pd.read_excel('test.xlsx')
df = df.apply(lambda x: pd.Series(x.dropna().values))
df = df.fillna('')
df.to_excel("test_modified.xlsx", index=False, header=False)
Any ideas how can achieve this?
Thank you.