且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将列表列表转换为字符串pandas dataframe

更新时间:2022-06-04 04:33:04

agg + join.逗号不在列表中,只是列表__repr__的一部分.

agg + join. The commas aren't present in your lists, they are just part of the __repr__ of the list.

str_cols = ['Before', 'After']

d = {k: ' '.join for k in str_cols}

df.agg(d).join(df.drop(str_cols, 1))

                        Before                     After  P_ID     Word N_ID
0       in the bright blue box  there are many different     1  crayons   A1
1  because they go really fast    i like a lot of sports     2     cars   A2
2         to ride and have fun  the middle east has many     3   camels   A3


如果您希望就位(更快):


If you'd prefer in place (faster):

df[str_cols] = df.agg(d)