gpt4 book ai didi

python - Dataframe.str 失败,因为整列都是空白

转载 作者:行者123 更新时间:2023-11-30 22:15:33 24 4
gpt4 key购买 nike

我有包含许多列的数据框。执行时我收到类似

的错误
Can only use .str accessor with string values, which use np.object_ dtype in pandas

我发现这是因为我的专栏在该特定情况下只有 Nan 值,并且 .str 在代码上失败了

df1['Merchant_old']=df1['Merchant_old'].str.lower().str.strip()

我不知道如何使用 isna() 仅当整个列不为空时才运行上述步骤。

最佳答案

我假设您的df.col类型是np.float。您可以将 strdf.col.astype(np.object_).str 一起使用。这会将 np.NaN 保留在最终输出中。

关于python - Dataframe.str 失败,因为整列都是空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50264801/

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