通过padding使Dataframe的列长度达到一定的字数

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

我的数据框看起来像

      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'。在 XYZ 列中,我想填充它,这样所有行的长度都是 max_words。

期望的输出

     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

第 3 行未填充,因为它的长度已经是 6。

IIUC,试试这个:

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

print(df)

输出:

     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