首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >numpy.where不适用于空字符串。

numpy.where不适用于空字符串。
EN

Stack Overflow用户
提问于 2022-01-27 13:34:16
回答 1查看 57关注 0票数 0

作为SEO管理人员,我使用这个python代码来查看H1标记在不同网页的桌面版和移动版上是否相同:

代码语言:javascript
复制
##Print the path of your current working directory
import os
print(os.getcwd())
#What you get here is where you should save your CSV crawls

##Import Panda Library
import pandas as pd
import numpy

##Load the crawls to Pandas
dfTextonly = pd.DataFrame(pd.read_csv('mobile.csv', low_memory=False, header=0))
dfTextonly = dfTextonly[['Address', 'H1-1']].copy()
dfJS = pd.DataFrame(pd.read_csv('desktop.csv', low_memory=False, header=0))
dfJS = dfJS[['Address','H1-1']].copy()
#Combine the two crawls into one dataframe
df = pd.merge(dfTextonly, dfJS, left_on='Address', right_on='Address', how='outer')

##Check the differences
df["H1s are equal"] = numpy.where((df["H1-1_y"] == df["H1-1_x"]), "yes", "no")
##Export in Excel
df.to_excel("test-results.xlsx")

但是,问题是,当H1-1_y和H1-1_x都是"nan“(空字符串)时,此代码中的numpy.where返回"no”值,而它应该返回"yes“,因为在这种情况下,它们是相同的。有人能帮我吗?

样本数据

单击此按钮下载示例数据

EN

回答 1

Stack Overflow用户

发布于 2022-01-27 15:02:19

如果是像你在评论中提到的那样处理NaN == NaN的话,你可以使用熊猫。代码看上去有点麻烦,所以您可以决定是否需要,但是可以尝试。

代码语言:javascript
复制
df["H1s are equal"] = pd.Series(["yes"]*len(df["H1-1_y"])).where(df["H1-1_y"]==df["H1-1_x"], "No")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70879440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档