【发布时间】: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