如何替换熊猫多索引列集中的值

时间:2020-08-11 10:47:39

标签: python pandas dataframe data-manipulation

给出以下内容:

dd = {}
for i in range(3):
    for j in range(3):
        key = (f"col_{i}", j)
        dd[key] = {1: 2, 3: 4}



print(pd.DataFrame.from_dict(dd))

外观如下:

  col_0       col_1       col_2      
      0  1  2     0  1  2     0  1  2
1     2  2  2     2  2  2     2  2  2
3     4  4  4     4  4  4     4  4  4

我想使用以下替换项:

reps = {
    "col_0": {0: "o", 1: "one", 2: "two"},
    "col_1": {0: "o2", 1: "one2", 2: "two2"},
    "col_2": {0: "o3", 1: "one3", 2: "two3"},
}

使col_0col_1col_2保持不变,但第二级 0,1,2更改为o, one, twoo2, one2, two2o3, one, two3 分别给出类似于:

  col_0             col_1            col_2      
      o  one  two   o2 one2 two2     o3  one3  two3
1     2  2    2      2  2    2        2    2     2
3     4  4    4      4  4    4        4    4     4

1 个答案:

答案 0 :(得分:1)

您可以使用列名称创建元组,然后将匹配字典用于document.getElementById("idOfScrollingSection").onscroll(() => { let scrollValue = document.getElementById("elementId").scrollTop; //Remove class from highlighted item let oldElement = document.getElementsByClassName("current"); oldElement.classList.remove("current"); //Add highlight class, change values depending on page position if (scrollValue < 200) { let elementToBeHighlighted = document.getElementById("idOfElementToBeHighlighted"); elementToBeHighlighted.classList.add("current"); } else if .... } else { let elementToBeHighlighted = document.getElementById("idOfElementToBeHighlighted"); elementToBeHighlighted.classList.add("current"); } }) ,并将第二个参数用作默认值,因此,如果不匹配,则不替换:

get

测试L = [(a, reps[a].get(b, b)) if a in reps else (a, b) for a, b in df.columns.tolist()] df.columns = pd.MultiIndex.from_tuples(L) print (df) col_0 col_1 col_2 o one two o2 one2 two2 o3 one3 two3 1 2 2 2 2 2 2 2 2 2 3 4 4 4 4 4 4 4 4 4 字典中是否没有匹配的外键:

reps

测试是否没有匹配的内部键:

reps = {
    "col_100": {0: "o", 1: "one", 2: "two"},
    "col_1": {0: "o2", 1: "one2", 2: "two2"},
    "col_2": {0: "o3", 1: "one3", 2: "two3"},
}

L = [(a, reps[a].get(b, b)) if a in reps else (a, b) for a, b in df.columns.tolist()]
df.columns = pd.MultiIndex.from_tuples(L)
print (df)
  col_0       col_1           col_2          
      0  1  2    o2 one2 two2    o3 one3 two3
1     2  2  2     2    2    2     2    2    2
3     4  4  4     4    4    4     4    4    4