【问题标题】:Select Rows where one cell contains 'some' word and store in a variable选择一个单元格包含“某些”单词并存储在变量中的行
【发布时间】:2021-11-29 18:15:23
【问题描述】:

我正在尝试分析 StackOverflow 调查数据,可以找到 here

我想在“Country”列中获取“United States”中包含的所有行,然后将值存储在变量中。 例如 =

my_data = `{'Age1stCode': 12, 13, 15, 16, 18
            'Country': 'India', 'China', 'United States', 'England', 'United States'
            'x': 'a', 'b', 'c', 'd', 'e'
}`

我想要的 =

my_result_data = `{'Age1stCode': 15, 18
            'Country': 'United States', 'United States'
            'x': 'c', 'e'
}`

【问题讨论】:

  • df[df['Country'] == 'United States']df.query('Country == "United States"')

标签: python pandas data-analysis


【解决方案1】:

如果您需要过滤或搜索数据框中特定列中的特定字符串 你只需要使用

df_query = df.loc[ df.country.str.contains('United States')]

您可以设置case = True 使搜索区分大小写

查看文档了解更多信息:pandas doc

【讨论】:

  • ValueError: Cannot mask with non-boolean array containing NA / NaN values。我明白了。 @Qaung Hoang 评论说对我来说效果很好。
  • 在应用此功能之前,您必须从 NAN 中清除数据
  • 它在附加的 CSV 文件上完美运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多