【问题标题】:How to get a name of Column or change the name of existing?如何获取列的名称或更改现有的名称?
【发布时间】:2016-09-03 17:36:30
【问题描述】:

我有一个任务是构建一个函数“removePunctuation”来去除标点符号,结果通过了这个测试:

# TEST Capitalization and punctuation (4b)
testPunctDF = sqlContext.createDataFrame([(" The Elephant's 4 cats. ",)])
testPunctDF.show()
Test.assertEquals(testPunctDF.select(removePunctuation(col('_1'))).first()[0],
                  'the elephants 4 cats',
                  'incorrect definition for removePunctuation function')

这是我设法写的。

def removePunctuation(column):
    """Removes punctuation, changes to lower case, and strips leading and trailing spaces.

    Note:
        Only spaces, letters, and numbers should be retained.  Other characters should should be
        eliminated (e.g. it's becomes its).  Leading and trailing spaces should be removed after
        punctuation is removed.

    Args:
        column (Column): A Column containing a sentence.

    Returns:
        Column: A Column named 'sentence' with clean-up operations applied.
    """

    return lower(trim(regexp_replace("column_name", "[\W_]+"," "))).alias("sentence");

但我仍然无法使函数 regexp_replace 使用别名“句子”。我收到此错误:

AnalysisException: u"cannot resolve 'sentence' given input columns: [_1];"

【问题讨论】:

    标签: python string apache-spark distributed-computing punctuation


    【解决方案1】:

    我会尝试:

    stringWithPunctuation.translate(None, string.punctuation)
    

    它在后台使用,在效率方面简直是最好的!


    你的尝试:

    return lower(trim(regexp_replace(, "[\W_]+"," "))).alias("sentence");
    

    似乎没有在任何地方使用参数column,这可以解释错误。

    【讨论】:

    • 哦,抱歉,我发布的代码有错误,在 regexp_replace() 第一个参数必须有 bean "column_name",反正我已经解决了,但是谢谢。
    • @DmitrijKostyushko 很高兴你解决了它!如果我知道您问题中的代码不是您使用的代码,我可能会发布一个更好的问题。记得稍后接受答案。 ;)
    【解决方案2】:

    令人惊讶的是,我能够在 regexp_replace() args 中传递列对象而不是列名。

    【讨论】:

      猜你喜欢
      • 2016-08-24
      • 2015-09-28
      • 2015-07-06
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2012-01-22
      • 2019-04-28
      相关资源
      最近更新 更多