如何筛选 URL 是否存在的火花数据帧



我想过滤我的火花数据帧。在此数据帧中,有一个 URL 列。

我尝试使用os.path.exists(col("url"))来过滤数据帧,但是我遇到了诸如

"需要字符串,但已找到列"。

这是我代码的一部分,熊猫已经在代码中使用了,现在我想用 Spark 来实现以下代码

bob_ross = pd.DataFrame.from_csv("/dbfs/mnt/umsi-data-science/si618wn2017/bob_ross.csv")
bob_ross['image'] = ""
# create a column for each of the 85 colors (these will be c0...c84)
# we'll do this in a separate table for now and then merge
cols = ['c%s'%i for i in np.arange(0,85)]
colors = pd.DataFrame(columns=cols)
colors['EPISODE'] = bob_ross.index.values
colors = colors.set_index('EPISODE')
# figure out if we have the image or not, we don't have a complete set
for s in bob_ross.index.values:
    b = bob_ross.loc[s]['TITLE']
    b = b.lower()
    b = re.sub(r'[^a-z0-9s]', '',b)
    b = re.sub(r's', '_',b)
    img = b+".png"
    if (os.path.exists("/dbfs/mnt/umsi-data-science/si618wn2017/images/"+img)):
        bob_ross.set_value(s,"image","/dbfs/mnt/umsi-data-science/si618wn2017/images/"+img)
        t = getColors("/dbfs/mnt/umsi-data-science/si618wn2017/images/"+img)
        colors.loc[s] = t
bob_ross = bob_ross.join(colors)
bob_ross = bob_ross[bob_ross.image != ""] 

这是我尝试使用 Spark 实现它的方式,我被困在错误线上

from pyspark.sql.functions import *
bob_ross = spark.read.csv('/mnt/umsi-data-science/si618wn2017/bob_ross.csv',header=True)
bob_ross=bob_ross.withColumn("image",concat(lit("/dbfs/mnt/umsi-data-science/si618wn2017/images/"),concat(regexp_replace(regexp_replace(lower(col('TITLE')),r'[^a-z0-9s]',''),r's','_'),lit(".png"))))
#error line ---filter----
bob_ross.filter(os.path.exists(col("image")))
print(bob_ross.head())

您应该使用过滤器功能,而不是操作系统功能

例如

df.filter("image is not NULL")

os.path.exists只在本地文件系统上运行,而Spark意味着在许多服务器上运行,所以这应该表明你没有使用正确的功能。

相关内容

最新更新