pandas 试图计算一个系列中每一行中每个单词的第一次出现次数

m1m5dgzv  于 2023-08-01  发布在  其他
关注(0)|答案(3)|浏览(116)

我有超过100行的调查数据,受访者输入了他们对开放式问题的回答。为了分析他们的回答,我想为每个问题创建一个单词云。我的想法是计算每个回答中的独特词,然后把它们加在一起,看看受访者说得最多的词。例如,如果应答者1说“愤怒”,应答者2说“愤怒”,则连接两个列表将导致单词“愤怒”出现两次。我希望避免的情况是,如果一个人多次使用一个词,我不希望他们多次使用这个词来扭曲数据。如果有人说了100次“愤怒”这个词,但没有人使用它,那么这个词并不能代表所有受访者的情绪。然后我会把这些数据输入到单词云程序中。
首先,我将Excel数据导入到pandas DataFrame中,然后分离出第一个问题的答案

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from wordcloud import WordCloud, STOPWORDS
  4. df = pd.read_excel("./Data - Open Ended Questions.xlsx", sheet_name="Working Data")
  5. df1 = df['question1']

字符串
接下来,我尝试遍历系列中的每个响应。下面的程序返回第一个响应中的唯一单词,但我不确定如何返回其他响应中的结果。我想我需要为每个响应创建一个列表,并将它们附加在一起,但我不太确定如何做到这一点。

  1. def words(df):
  2. l = list()
  3. for index, response in df.items():
  4. response = response.lower()
  5. words = response.split()
  6. for word in words:
  7. if word in l:
  8. l
  9. else:
  10. l.append(word)
  11. return l
  12. words(df1)


用户Zombro用他们的回应引导我走上了正确的道路。我的最终解决方案如下。

  1. def unique_words_per_response(series):
  2. # convert to lowercase to combine words like "The" and "the"
  3. series = series.str.lower()
  4. # remove punctuation using the String package
  5. series = series.str.translate(response.maketrans("", "", string.punctuation))
  6. # Zombro's solution
  7. series = series.apply(lambda series: pd.Series(series.split()).unique()).explode()
  8. # convert words to capitalize first letter of each word to look better in word cloud
  9. series = series.str.capitalize()
  10. # join words with spaces to create one single string
  11. series = ' '.join(series.tolist())
  12. return series

wvt8vs2t

wvt8vs2t1#

你在寻找这样的解决方案吗?用一些示例数据集来澄清您的问题会有所帮助。

  1. import pandas as pd
  2. s = pd.Series([
  3. "example response 1",
  4. "another response",
  5. "another example response",
  6. "vanilla bean"
  7. ])
  8. s.apply(lambda s: s.split()).explode().value_counts()
  9. >>>
  10. response 3
  11. example 2
  12. another 2
  13. 1 1
  14. vanilla 1
  15. bean 1
  16. dtype: int64

字符串
我认为此解决方案的关键是对数据进行非标准化,然后利用Series.explodehttps://pandas.pydata.org/docs/reference/api/pandas.Series.explode.html

展开查看全部
bsxbgnwa

bsxbgnwa2#

你可以从每一行中得到第一个单词,

  1. df['question1'].apply(lambda x:x.split()[0])

字符串
编辑-1:
根据OP的注解,得到唯一的单词,没有标点符号

  1. data = {'q1': ["Hello, world!",
  2. "This is a sample sample sentence.",
  3. "Pandas Pandas is great!"]}
  4. df = pd.DataFrame(data)
  5. translator = str.maketrans('', '', string.punctuation)
  6. unique_words = set(" ".join(df['q1'].str.lower().str.translate(translator).values).split())

输出:

  1. {'hello', 'pandas', 'this', 'a', 'great', 'is', 'sample', 'sentence', 'world'}

展开查看全部
chhkpiq4

chhkpiq43#

是的,要为每个问题创建单词云,您需要分别处理每个回答,然后合并结果。首先,将return语句移到循环之外,实际返回所有响应,而不仅仅是第一个。现在,让我们试着修改你的代码和平;

  1. for index, response in df.items():

字符串
然后,看看循环本身,我们需要遍历df中的项,最后我们只是。

  1. response = response.lower()


在这个循环中,我们将响应转换为小写,以确保字频率计数不区分大小写,

  1. words = response.split()
  2. unique_words.append(set(words))


在我们从我们得到的响应中创建一个单词列表之后,最后我们将结果添加到列表变量l(如果你使用set(),它将删除重复的单词)。

  1. combined_words_set = set().union(*unique_words)


当我们得到我们的话,我们做了一点操作,具体地说,我们需要将所有的集合组合成一个集合,

  1. unique_words_list = list(combined_words_set)


之后,我们通过设置成一个列表来达到最终结果。我只能说这些,希望对你有所帮助)
代码应该像这样:

  1. def words(df):
  2. unique_words = [] # rename l into unique_words for clearity
  3. for index, response in df.items():
  4. response = response.lower()
  5. words = response.split()
  6. unique_words.append(set(words))
  7. combined_words_set = set().union(*unique_words)
  8. unique_words_list = list(combined_words_set)
  9. return unique_words_list
  10. unique_words_question1 = words(df1)

展开查看全部

相关问题