pythonpandasdataframepad

Make Dataframe column length to a certain number of words by padding


My dataframe looks like

      Abc                       XYZ 
0  Hello      How are you doing today
1   Good                    This is a
2    Bye                      See you
3  Books     Read chapter 1 to 5 only

max_words = 6, filler_word = 'end'. In column XYZ, i want to pad it, such that all the rows are max_words in length.

Desired Output

     Abc                       XYZ
0  Hello               How are you end end end
1   Good               This is a end end end
2    Bye               See you end end end end
3  Books               Read chapter 1 to 5 only

Row 3 is not filled, as it's length was already 6.


Solution

  • IIUC, try this:

    df['XYZ'] = df['XYZ'].str.split(expand=True)\
                         .fillna('end')\
                         .apply(lambda x: x.str.cat(sep=' '), axis=1)
    
    print(df)
    

    Output:

         Abc                          XYZ
    0  Hello  How are you doing today end
    1   Good        This is a end end end
    2    Bye      See you end end end end
    3  Books     Read chapter 1 to 5 only