【问题标题】:Group rows in Pandas dataframe, apply custom function and store results in a new dataframe as rows [closed]在 Pandas 数据框中对行进行分组,应用自定义函数并将结果作为行存储在新的数据框中 [关闭]
【发布时间】:2021-08-29 15:14:48
【问题描述】:

我有一个带有三列的 pandas 数据框 df_org - 索引(整数)、标题(字符串)和日期(日期)。

我有一个方法process_title(text),它将一个字符串作为输入并标记化,删除停用词并将输入字符串进行词形还原并将单词作为列表返回。

from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()

def process_title(text):
    tokens = word_tokenize(text.lower())
    try:
        tokens.remove("google")
        tokens.remove("search")
        tokens.remove("-")
    except:
        pass

    lemm_tokens = list(map(lemmatizer.lemmatize,tokens))
    without_stop = [word for word in lemm_tokens if word not in stop_words]
    return without_stop

我想要一个包含三列的新数据框 - 字(字符串)、频率(整数)、日期(日期)。 Word 列包含 process_title(text) 返回的列表中的单词(单个单词),Frequency 列包含该单词在给定日期出现的频率和 日期列包含日期。

    ---------------------------------------
    |  Word    | Frequency     |   Date   |
    ---------------------------------------
    | computer | 1             |2021-08-01|
    | science  | 1             |2021-08-01|
    | something| 5             |2021-08-02|
.....

如何将 df_org 数据框按日期分组并创建新数据框?可以在不影响最终要求的情况下对 process_title(text) 方法进行更改。

【问题讨论】:

  • 请发布一些您的数据样本和预期输出以及您迄今为止尝试过的内容
  • 确实如此。请提供minimal reproducible example

标签: python pandas dataframe nltk data-analysis


【解决方案1】:

你可以使用DataFrame.explode方法,后跟groupbysize

我将只使用一个简单的.str.split 而不是你的函数,因为我不知道word_tokenize 来自哪里。

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'title': ['Hello World', 'Foo Bar'], 'date': ['2021-01-12T20:00', '2021-02-10T22:00']})

In [3]: df['words'] = df['title'].apply(lambda s: process_title(str(s)))

In [4]: df
Out[4]:
         title              date           words
0  Hello World  2021-01-12T20:00  [Hello, World]
1      Foo Bar  2021-02-10T22:00      [Foo, Bar]

In [5]: exploded = df.explode('words')

In [6]: exploded
Out[6]:
         title              date  words
0  Hello World  2021-01-12T20:00  Hello
0  Hello World  2021-01-12T20:00  World
1      Foo Bar  2021-02-10T22:00    Foo
1      Foo Bar  2021-02-10T22:00    Bar

In [7]: exploded.groupby(['date', 'words']).size()
Out[7]:
date              words
2021-01-12T20:00  Hello    1
                  World    1
2021-02-10T22:00  Bar      1
                  Foo      1
dtype: int64

【讨论】:

  • 能否请您修改代码以使用 process_title() 方法?我使用了 nltk 库。该方法返回一个字符串列表。
  • str.split也是,所以应该直接适用
  • df['words'] = df['title'].apply(lambda s: process_title(str(s))) 成功了
猜你喜欢
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多