【问题标题】:Write a function that returns the count of the unique answers to all of the questions in a dataset [duplicate]编写一个函数,返回数据集中所有问题的唯一答案的计数[重复]
【发布时间】:2021-07-04 00:19:21
【问题描述】:

例如,在将整个数据集过滤为仅包含“King”一词的问题后,我们就可以找到这些问题的所有唯一答案。

我使用以下代码过滤:

`def lower1(x):
   x.lower()

 filter_dataset = lambda x:all(x) in jeopardy.Question.apply(lower1) 
 print(filter_dataset(['King','England']))`

上面的代码打印True,而不是打印带有关键字“King”和“England”的jeopardy['Question']行。

这是第一个问题。

现在我想计算危险的唯一答案['Question']

这是示例数据框

现在我想创建一个计算唯一答案的函数。 我写了以下代码:

  `def unique_counts():
      print(jeopardy['Answer'].unique().value_counts())
   unique_counts()`

这给了我以下错误:

AttributeError: 'numpy.ndarray' 对象没有属性 'value_counts'

【问题讨论】:

  • 失败是什么意思??
  • 为子字符串检查添加了更好的欺骗。

标签: python-3.x pandas dataframe data-analysis


【解决方案1】:

使用Series.str.contains:

jeopardy[jeopardy['Question'].str.contains('|'.join(['King','England']))]

【讨论】:

  • 答案已关闭。
  • 如果这是他想要的,让我们等待 OP 卷土重来。如果他想要别的东西,可能需要重新提出问题。
  • 我不知道为什么,但问题已关闭。
  • @KarthikBhandary - isin 需要什么?还是别的什么?
  • 我是说我想从jeopardy.Question 创建一个数据集,我使用了isin,但它创建了一个空数据集。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 2021-03-02
  • 1970-01-01
相关资源
最近更新 更多