Concatenate cells into a string with separator pandas python

前端 未结 5 1879
暖寄归人
暖寄归人 2021-02-14 16:53

Given the following:

df = pd.DataFrame({\'col1\' : [\"a\",\"b\"],
            \'col2\'  : [\"ab\",np.nan], \'col3\' : [\"w\",\"e\"]})

I would l

5条回答
  •  温柔的废话
    2021-02-14 17:15

    In [1556]: df.apply(lambda x: '*'.join(x.dropna().astype(str).values), axis=1)
    Out[1556]: 
    0    a*ab*w
    1       b*e
    2     3*4*�
    3     ñ*ü*á
    dtype: object
    

提交回复
热议问题