我有一个这样的DataFrame
A B C D "1" "2" "3" "4" "-" "5" "8" "-" "5" "-" "1" "-"
“中的值表示所有df均为object类型。
如何将int部分转换为int并照常保留“-”?输出应为
A B C D 1 2 3 4 "-" 5 8 "-" 5 "-" 1 "-"
您可以在使用pd.to_numeric进行重塑后尝试pd.to_numeric,然后再返回stack
pd.to_numeric
stack
unstack