Remove Empty Cells And Move Content To The Left Python

[Solved] Remove Empty Cells And Move Content To The Left Python | Vb - Code Explorer | yomemimo.com
Question : Remove empty cells and move content to the left Python

Answered by : juan-andres-russy-cervera

Ok, then I propose to transpose the dataset, process it, and transpose it back.
import pandas as pd
import numpy as np
df = pd.DataFrame( { "A": ["ABC", "DEF", "XYZ"], "B": ["XLS", "XLS", "XLS"], "C": ["1231341231", "1231231231", "1231231233"], "D": ["123123asdad1923", "1231823asda9123", "2138820394832sd"], "E": [np.nan, np.nan, np.nan], "F": [np.nan, "askda213", np.nan], "I": ["blabla", "blabla", np.nan], "K": ["123123asdad1923", "123123asdad1923", "asdasdq2ew12332"], }
)
print(df)
# transpose
df_transposed = df.T
# move NaN at end of column
df_transposed = df_transposed.apply(lambda x: pd.Series(x.dropna().values))
# replace Nan by empty strings
df_transposed.replace(np.nan, "", inplace=True)
# transpose back
df = df_transposed.T
print()
print(df)

Source : https://stackoverflow.com/questions/71055611/remove-empty-cells-and-move-content-to-the-left-python | Last Update : Tue, 16 Aug 22

Answers related to remove empty cells and move content to the left python

Code Explorer Popular Question For Vb