如何在python中将字符串转换为null或Nan?
我有一个表,其中一列有string和integer,我想将string转换为integer为0。如何在python中实现这一点? 该列类似如下所示:如何在python中将字符串转换为null或Nan?,python,pandas,Python,Pandas,我有一个表,其中一列有string和integer,我想将string转换为integer为0。如何在python中实现这一点? 该列类似如下所示: size 1 1 1 2 ABC 我希望它可以转换为: size 1 1 1 2 0 与errors='concurve'一起使用,对于缺少的值而不是字符串,将其替换为并最后转换为整数: df['size'] = pd.to_numeric(df['size'], errors='coerce').fillna(0).astype(int) p
size
1
1
1
2
ABC
我希望它可以转换为:
size
1
1
1
2
0
与errors='concurve'
一起使用,对于缺少的值而不是字符串,将其替换为并最后转换为整数:
df['size'] = pd.to_numeric(df['size'], errors='coerce').fillna(0).astype(int)
print (df)
size
0 1
1 1
2 1
3 2
4 0
您可以尝试
isinstance()
函数吗
test=["abc",1,2,3,"def"]
for x in test:
if isinstance(x, str): print ("0")
else: print (x)
输出:
0
1
2
3
0