【问题标题】:Creating new columns with value based on dictionary keys基于字典键创建具有值的新列
【发布时间】:2020-04-06 12:09:07
【问题描述】:

我有一个数据框和一个字典:

news = {'Text':['dog ate the apple', 'cat ate the carrot', 'dog drank water'], 'Source':['NYT', 'WP', 'Guardian']}
news_df = pd.DataFrame(news)


w = {1:['horse', 'dog'], 2:['apple'], 10: ['water', 'melon', 'liquerice']}

我想创建一个新列 news_df['sum'] 来查看 news_df['Text'],检查是否有任何字典值可用,如果列中有 1 个或多个,则分配总和按键。我的结果是:

results = {'Text':['dog ate the apple', 'cat ate the carrot', 'dog drank water'], 'Source':['NYT', 'WP', 'Guardian'], 'sum' : [3, 0, 11]}
results_df = pd.DataFrame(results)

知道怎么做吗?我不确定采取什么方法?也许将字典变成数据框?

【问题讨论】:

    标签: python pandas dictionary mapping


    【解决方案1】:

    这是一个应用方法:

    def counts(x): 
       sumcount = 0 
       for k, v in w.items(): 
          for word in v: 
            if word in x: 
               sumcount+=int(k) 
       return sumcount 
    
    news_df.Text.apply(counts)
    
                     Text    Source  sum
    0   dog ate the apple       NYT    3
    1  cat ate the carrot        WP    0
    2     dog drank water  Guardian   11
    

    【讨论】:

    • 不确定为什么会出现以下错误?类型错误:+= 不支持的操作数类型:“int”和“str”
    • @FilippoSebastio,我刚刚更新并将 k 包装为 int,它是您数据集中的字符串
    猜你喜欢
    • 2019-11-17
    • 2017-08-03
    • 2018-10-07
    • 2022-01-24
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多