You can call apply pass axis=1 to apply row-wise, then convert the dtype to str and join:In [153]:df['ColumnA'] = df[df.columns[1:]].apply( lambda x: ','.join(x.dropna().astype(str)), axis=1)dfOut[153]: Column1 Column2 Column3 Column4 Column5 ColumnA0 a 1 2 3 4 1,2,3,41 a 3 4 5 NaN 3,4,52 b 6 7 8 NaN 6,7,83 c 7 7 NaN NaN 7,7在这里,我打电话给dropna以摆脱NaN,但是我们需要再次强制转换为int,因此我们不会以浮点数作为str结束.Here I call dropna to get rid of the NaN, however we need to cast again to int so we don't end up with floats as str. 这篇关于将多个列值合并到python pandas的一列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 03:31