Right now I have a DF like this
Word Word2 Word3
Hello NaN NaN
My My Name NaN
Yellow Yellow Bee Yel
Since you want the values to move up, you'll have to create a new data frame
Started with -
Word Word2
0 Hello NaN
1 My My Name
2 Yellow Yellow Bee
3 Golden Golden Gates
4 Yellow NaN
Used following method -
def get_column_array(df, column):
expected_length = len(df)
current_array = df[column].dropna().values
if len(current_array) < expected_length:
current_array = np.append(current_array, [''] * (expected_length - len(current_array)))
return current_array
pd.DataFrame({column: get_column_array(df, column) for column in df.columns}
Gives -
Word Word2
0 Hello My Name
1 My Yellow Bee
2 Yellow Golden Gates
3 Golden
4 Yellow
You can also edit the existing df with the same function -
for column in df.columns:
df[column] = get_column_array(df, column)