连接 noneType 和字符串值列(pandas 数据帧)导致 "NaN"

Concatenating noneType and string value columns (pandas dataframes) results in "NaN"

我正在尝试连接两列,而第二列有几个 noneType 值。当我尝试将两个列与 noneType 值连接时,生成的列结果为 "NaN".

我试图环顾四周,看看是否可以找到有关此行为的问题,但我找不到。

这是 table 在串联之前的样子:

这是我修改后加入两列的代码:

new_table["name"] = new_table[0] + new_table[1]

结果是:

为什么串联会导致 "NaN",我该如何解决?

最简单的修复方法是将 None 替换为空字符串:

new_table["name"] = new_table[0] + new_table[1].fillna('')
df = pd.DataFrame([["K.", "Mbappe"], ["N.", np.nan]])
print (df)

输出:

    0       1  
0  K.  Mbappe  
1  N.     NaN  


df['Name'] = df[0].str.cat(df[1], na_rep='')
print(df)

输出:

    0       1      Name
0  K.  Mbappe  K.Mbappe
1  N.     NaN        N.

这与 ypnos 提出的方法相同,而是使用系列 str.cat 函数。