下面是我的代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({ 'object': ['a', 'b', 'c',np.nan],
'numeric': [1, 2, np.nan , 4],
})
df['both'] = df['object'] + '__' + df['numeric'].astype(str)
运行后是df这样的:
object numeric both
a 1 a__1.0
b 2 b__2.0
c nan c__nan
nan 4 nan
在上面的列中,both我需要而不是NaN添加任何内容,并且列中添加的数字both应该看起来与numeric列中的一样(不添加.0等)。所以,我想得到:
object numeric both
a 1 a__1
b 2 b__2
c nan c__
nan 4 4__
qq_花开花谢_0
相关分类