python - 使用 MultiIndex 在数据框中插入新列值?

标签 python pandas jupyter

我有一个看起来像这样的 Pandas 数据框:

          rank     num      rank    num
          2015     2015     2014    2014   
France    8        1200     9       1216   
Italy     11       789      10      788    

我想在多索引中添加一个新的列,叫做corrected_num,我希望这个列的值是对应的num的值行,除以从另一个字典中获得的数字,如下所示:

b = {2015: 10, 2014: 12}

换句话说,我想以这样的方式结束:

          rank     num      num_corrected    rank    num    num_corrected
          2015     2015     2015             2014    2014   2014
France    8        1200     120              9       1216   101.3
Italy     11       789      78.9             10      788    65.6

到目前为止,我的方法是遍历数据框中的每一行,然后遍历行中的每一列,如下所示:

for i, row in df.iterrows():
    for year in df.num.columns:
        df.set_value(i, 'num_corrected, ' + year, row.frequency[year] / b[year])

但是当我尝试这个时,我的 Jupyter notebook 死机了,所以我希望有更好的方法!

最佳答案

设置

df = pd.DataFrame(
    [
        [8, 1200, 9, 1216],
        [11, 789, 10, 788]
    ],
    ['France', 'Italy'],
    pd.MultiIndex.from_product([['rank', 'num'], [2015, 2014]])
).sort_index(axis=1, level=1)

b成为一个系列

b = pd.Series({2015: 10, 2014: 12})

方法一

num_c = df.num / b

cols = num_c.columns
num_c.columns = [['num_corrected'] * len(cols), cols]

pd.concat([df, num_c], axis=1)

enter image description here

方法二

d1 = df.stack()
d1['num_corrected'] = d1.num / d1.index.get_level_values(1).to_series().map(b).values
d1.unstack().sort_index(axis=1, level=1)

enter image description here

关于python - 使用 MultiIndex 在数据框中插入新列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41187684/

相关文章:

python - Keras 将类别预测与标签合并

python - Pandas - 附加到 DataFrame 时如何控制列顺序

jupyter-notebook - 无法显示类型为 HBox 的 Jupyter Widget;小部件 JavaScript 库丢失?

python - OpenCV 在 Jupyter Notebook 中出现错误,但在 Python CLI 中有效

python - Python 中高效的逐元素函数计算

python - 将一个列表中随机选择的元素插入另一个列表

python - pandas IndexError/TypeError 与 NaN 值不一致

python - 使用过滤后的行创建新的数据框

python - 将元组的元组变成字典

python - 有没有办法更改 Pandas 数据透视表的边距 "All"列位置?