【问题标题】:Python find nth item from a line of text in a dataframePython从数据框中的一行文本中查找第n个项目
【发布时间】:2021-06-29 10:57:47
【问题描述】:

我有一串文本,我想从中找到第 n 个单词。我可以通过 min 和 max 提取第一个和最后一个,但不知道如何获取中间的项目。

我的代码:

import pandas as pd
import numpy as np
data = {"Text" : ["['one', 'one two', 'four']","['two one', 'three', 'five]"]}
df = pd.DataFrame(data)

df["One"] = df["Text"].str.find("one")
df["Two"] = df["Text"].str.find("two")
df["Three"] = df["Text"].str.find("three")
df["Four"] = df["Text"].str.find("four")
df["Five"] = df["Text"].str.find("five")

score_words = df.loc[:,"One":"Five"]
score_words_dict = dict(
    list(
        score_words.groupby(score_words.index)
    )
)

score_words = score_words[score_words >0]

df["AllScoreWords"] =""
for k, v in score_words_dict.items():               # k: name of index, v: is a df
    df["AllScoreWords"][k] = str(v.columns[(v != -1).any()].to_list())

df['First_Score'] = score_words.idxmin(axis=1)
df['Last_Score'] = score_words.idxmax(axis=1)

print(df)
print(score_words)

所以在第一行中,我希望能够提取 Two 作为第二个记分词 & 在第二行中我想提取 One 作为第二个记分词.....等等。

实际上我有一个关键字紧随其后或之前我想拉出所说的话,所以简单地增加得分词的阈值是行不通的。

我怎样才能挑选出我想要的单词?
J

【问题讨论】:

    标签: python string dataframe text


    【解决方案1】:

    发现答案是这样使用替换删除元素的:

    scorewords_table["Clean_ScoreWords"] =scorewords_table.AllScoreWords.str.replace("[","")
    

    This answer 然后告诉我如何到达那里:

    df['V'] = df['V'].str.split('-').str[0]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 2012-01-10
      • 1970-01-01
      • 2021-04-22
      • 2016-03-21
      • 2019-07-19
      相关资源
      最近更新 更多