Python Pandas读取\u csv数据类型转换列不正确
如果我有以下CSVPython Pandas读取\u csv数据类型转换列不正确,python,pandas,Python,Pandas,如果我有以下CSV "1" "2" "23" 我读了 names = ["nullable"] dtype = [("nullable", 'int32')] df = pd.read_csv(r"E:\work\nullable.csv", names=names, dtype=dtype, encoding = "utf-8") 查看df.info(): 然而,如果我这样做 s = pd
"1"
"2"
"23"
我读了
names = ["nullable"]
dtype = [("nullable", 'int32')]
df = pd.read_csv(r"E:\work\nullable.csv",
names=names,
dtype=dtype,
encoding = "utf-8")
查看df.info()
:
然而,如果我这样做
s = pd.Series([1, 2.0, np.nan, 4.0])
s2 = s.astype('Int32')
dtype
正确填写为Int32
s2.info()
AttributeError("'Series' object has no attribute 'info'")
s2
0 1
1 2
2 NaN
3 4
dtype: Int32
对我来说这看起来像个虫子
对于如何解决这个问题,有什么建议吗?因为我想将CSV保存为拼花地板,但是如果我使用pd.Int32Dtype
该列将保存为字符串
删除或替换
NaN
s是不可行的。Pandas read\u csv将“NaN”解释为Null,但不是“NaN”。您可以将“NAN”传递给na_值参数
df = pd.read_csv(r"E:\work\nullable.csv",
names=names,
dtype=dtype,
encoding = "utf-8",
na_values = 'NAN'
)
对不起,楠的表情一定把你弄糊涂了。我已经更新了问题。我正在运行“0.25.0”并使用pd.Int32Dtype,效果与预期一样。也许你的版本有问题?
s2.info()
AttributeError("'Series' object has no attribute 'info'")
s2
0 1
1 2
2 NaN
3 4
dtype: Int32
df = pd.read_csv(r"E:\work\nullable.csv",
names=names,
dtype=dtype,
encoding = "utf-8",
na_values = 'NAN'
)