【问题标题】:Python Pandas iterate through list and mapPython Pandas 遍历列表和映射
【发布时间】:2022-01-19 21:19:23
【问题描述】:

我正在尝试制作一个简单的脚本,它应该获取项目的 id 并获取一些与之关联的统计数据并将其添加到数据框中。

stats = ['info_attack', 'info_defense', 'info_magic', 'info_difficulty', 'stats_hp', 'stats_hpperlevel', 'stats_mp', 'stats_mpperlevel',
         'stats_movespeed', 'stats_armor', 'stats_armorperlevel', 'stats_spellblock', 'stats_spellblockperlevel', 'stats_attackrange',
         'stats_hpregen','stats_hpregenperlevel', 'stats_crit', 'stats_critperlevel','stats_attackdamage','stats_attackdamageperlevel',
         'stats_attackspeedperlevel', 'stats_attackspeed']

ngrams = pd.concat([champItemMonoCombinations,champItemBiCombinations,champItemTriCombinations, champItemFourCombinations, champItemPentaCombinations,champItemSexCombinations ])



for stat in stats:
    print(stat)
    ngrams[stat] = ngrams['ChampionID'].map(champDataFrame.set_index('key')[stat])

它返回 nan 我不知道为什么

编辑:我将存储在一个数据帧中的整数与另一个数据帧中的字符串进行比较。现在效果很好

【问题讨论】:

    标签: python pandas mapping


    【解决方案1】:

    尝试使用.replace 而不是.map

    for stat in stats:
        print(stat)
        ngrams[stat] = ngrams['ChampionID'].replace(champDataFrame.set_index('key')[stat])
    

    【讨论】:

      猜你喜欢
      • 2012-05-14
      • 2016-12-08
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2016-05-03
      相关资源
      最近更新 更多