How to justify (left) a column of strings in pandas dataframe?

Viewed 3102

I have a dataframe with several columns. I use Jupyter. One of the columns is just strings, the particular column name is empty ' ':

df = pd.DataFrame([[""], [""], [""], ['*'], ['* <<']],
                  columns=[''],
                  index=[0, 1, 2, 3, 4])

print(df)

0    
1    
2    
3   * <<
4      *

How can I justify left, so it looks like this:

print(df)

0    
1    
2    
3   * <<
4   *

Note: if I just use df, instead of print(df), it justifies well.

1 Answers

Option 1
Use pd.Series.str.ljust

df.iloc[:, 0] = (lambda s: s.str.ljust(s.str.len().max()))(df.iloc[:, 0])
df

0      
1      
2      
3  *   
4  * <<
Related