【问题标题】:Search for index value in a dataframe在数据框中搜索索引值
【发布时间】:2020-06-29 09:03:53
【问题描述】:

我正在寻找根据索引值设置不同的条件。

我有以下索引值:

country

Uk
Us
Es
In
It
Ge
Ho

country 在我的数据框中是一个索引。 我需要做以下事情

  • 如果索引值等于 'Uk' 则做某事;
  • 如果索引值等于“我们”,则执行其他操作;

等等。

我已经尝试如下

 if df.index.isin(['Us']) or df.isin(['Uk']):
        stop_words = stopwords.words('english')
    if df.index.isin(['Es']):
        stop_words = stopwords.words('spanish')  

但这是错误的方法。我不熟悉熊猫数据框中的索引,因为我一直使用列。 感谢您的帮助和建议。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以使用 .loc() 选择数据框的索引

    df.loc['US', 'stop_words'] = 'english'
    df.loc['UK', 'stop_words'] = 'english'
    df.loc['ES', 'stop_words'] = 'spanish'
    

    此示例将根据索引创建一个新列stop_words,其中包含英语或西班牙语。

    【讨论】:

    • 谢谢伊波利托。我不需要创建新列,只需单独定义停用词列表以供以后使用。是否可以将英国和美国组合在一起,或者我需要单独进行?
    • 我收到了这个错误:TypeError: Cannot index by location index with a non-integer key
    • 他的意思是 loc 而不是 iloc: df.loc[['US','UK'], 'stop_words'] = 'english'
    • 关于truth value 的事情有几个主题。简而言之,您不应该像在代码中那样做if some_series or some_series。原因是if,作为 Python 语句只接受一个布尔值,因此它不会检查您的系列中的各个行/单元格。
    • 一个简单的df.loc[['US', 'UK']] 将为您提供由USUK 索引的所有行。如果这不是你想要的,你需要用更多你想要的细节来更新你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多