【问题标题】:Define few dependable functions inside one. Python在一个内部定义几个可靠的函数。 Python
【发布时间】:2019-03-18 20:18:41
【问题描述】:

例如,数据框是:

df = pd.DataFrame(data = {'id': ['393848', '30495'],
                         'text' : ['This is Gabanna. @RT Her human Jose rushed past firefighters into his burning home to rescue her. She suffered burns on her nose and paws, but will be just fine. The family lost everything else. You can help them rebuild below. 14/10 for both (via @KUSINews)',
                                  'Meet Milo. He’s a smiley boy who tore a ligament in his back left zoomer. The surgery to fix it went well, but he’s still at the hospital being monitored. He’s going to work very hard to fetch at full speed again, and you can help him do it below. 13/10']
                         })

我写了一些函数:

def tokenize(df): 
    def process_tokens(df): #return column with lists of tokens
        def process_reg(text): #return plain text
            return " ".join([i for i in re.sub(r'[^a-zA-Z\s]', "", str(text)).split()])
        df['tokens'] = [process_reg(text).split() for text in df['text']]
    return process_tokens(df) 

tokenize(df)

def process(df): #return column with dicts
    def process_group(token): #convert list of tokens into dictionery
            return pd.DataFrame(token, columns=["term"]).groupby('term').size().to_dict()
    df['dic'] = [process_group(token) for token in df['tokens']]

process(df)

他们一个接一个地工作得很好,我得到了预期:

我正在寻找将所有函数嵌套到一个中以便能够只传递一次数据帧的解决方案。

找不到。

请帮忙

【问题讨论】:

  • process(tokenize(df))?
  • --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-20-4af9fb28a669> in <module>() ----> 1 process(tokenize(df)) <ipython-input-15-ae9d3f8238ed> in process(df) 2 def process_group(token): #convert list of tokens into dictionery 3 return pd.DataFrame(token, columns=["term"]).groupby('term').size().to_dict() ----> 4 df['dic'] = [process_group(token) for token in df['tokens']] TypeError: 'NoneType' object is not subscriptable
  • 编写一个函数,调用tokenize,然后调用process
  • 所以我还需要一个功能?你能举个例子吗?以及如何将它们全部嵌套到一个容器中?
  • 嗯,在我看来,您想要一个您还没有的功能,因此您需要一个更多功能来获得您想要的功能。现在你不知道如何将tokenize(df);process(df) 放入函数中?

标签: python pandas function stringtokenizer


【解决方案1】:
def ad (df):
    def tokenize(df): #return column with dicts
        def process_tokens(df): #return column with lists of tokens
            def process_reg(text): #return plain text
                return " ".join([i for i in re.sub(r'[^a-zA-Z\s]', "", str(text)).split()])
            df['tokens'] = [process_reg(text).split() for text in df['text']]
        return process_tokens(df)

    tokenize(df)

    def process (df):
        def process_dic(df): #return column with dicts
            def process_group(token): #convert list of tokens into dictionery
                return pd.DataFrame(token, columns=["term"]).groupby('term').size().to_dict()
            df['dic'] = [process_group(token) for token in df['tokens']]
        return process_dic(df)

    return process(df)

那么……

ad(df)

效果很好。虽然我有一个想法,另一种编写方式会执行得更快....另一天的挑战。

感谢您的支持,@Goyo!

【讨论】:

  • 你不需要把tokenizeprocess的定义放在ad里面。 Flat is better than nested.
  • 技术上同意,但这个是我自己使用的,我个人想要一个功能来处理所有这些东西。我试图把一个放在另一个里面,但失败了,我在这里提出了这个问题。我知道它现在并不完美)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 2014-05-21
  • 2012-04-25
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多