gpt4 book ai didi

python - Pandas : "ValueError: could not convert string to float: "(空字符串)

转载 作者:行者123 更新时间:2023-12-05 04:00:31 27 4
gpt4 key购买 nike

尝试将 Pandas 系列转换为 float 时,我收到此错误消息。它引用一个空字符串 (""),它不是 np.nan。只是一个空字符串。

我尝试用 np.nan 替换 "",但它没有做任何事情。我也尝试用“”替换“”,但它也没有做任何事情。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [4, " ", 6], [7, 8, 9]]),
columns=['a', 'b', 'c'])

df.astype(float)

ValueError: could not convert string to float:

我想在大型数据框中用 NaN 值替换这些“”。

最佳答案

您可以使用 df.replace() 函数来执行此操作

df.replace(r'^\s*$', np.nan, regex=True)

关于python - Pandas : "ValueError: could not convert string to float: "(空字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56171339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com