多索引数据框行替换

时间:2018-07-20 09:04:05

标签: python pandas

我有两个DataFrame Multi-indexed。 一个是我的引用(大约37000行),另一个是包含较少的值(例如ex 10)

我想用第二个值替换大的行

例如:df1 :(很抱歉,显示效果不佳)

lvl1    lvl2    lvl3    Value   Value2

A       1   I   0,862877333 0,181795348
        1   II  0,787022218 0,292046262
        1   III 0,40516176  0,445079108
        2   I   0,882167166 0,683954412
        2   IV  0,743618024 0,103097267
        3   I   0,901062673 0,729188996
        3   II  0,529989452 0,715379923
        3   IV  0,740272198 0,792457421
 B      1   I   0,548587694 0,637462653
        1   II  0,201284924 0,084391963
        2   I   0,999118031 0,558207224
        2   II  0,63353019  0,251377184
        2   V   0,694294638 0,685050861
        3   V   0,436723389 0,310871641
        3   VI  0,630832871 0,869957421
        3   VII 0,157874482 0,639308814

df 2

   lvl1 lvl2 lvl3 Value Value2
   A    1   I   0,8654  1
   B    2   II  0,264   2

产生的df3

lvl1    lvl2    lvl3    Value   Value2

A       1   I   **0,8654**  0,181795348
        1   II  0,787022218 0,292046262
        1   III 0,40516176  0,445079108
        2   I   0,882167166 0,683954412
        2   IV  0,743618024 0,103097267
        3   I   0,901062673 0,729188996
        3   II  0,529989452 0,715379923
        3   IV  0,740272198 0,792457421
        1   I   0,548587694 0,637462653
B       1   II  0,201284924 0,08439196
        2   I   0,999118031 0,558207224
        2   II  **0,264**   0,251377184
        2   V   0,694294638 0,685050861
        3   V   0,436723389 0,310871641
        3   VI  0,630832871 0,869957421
        3   VII 0,157874482 0,639308814

2 个答案:

答案 0 :(得分:0)

您也许可以使用pd.merge

import numpy as np
import pandas as pd
temp = pd.DataFrame({"lvl1": ["A","A","B","B"], "lvl2": [1,2,1,2], "lvl3":  ["I","II","I","II"], "Value": [0.8628773,0.7870, 0.63353, 0.6998]})
replace = pd.DataFrame({"lvl1": ["A","B"], "lvl2": [1,2], "lvl3": ["I","II"], "Value": [0.8654, 0.264], "Value2": [1,2]})
df = pd.merge(temp, replace, how="left", on=["lvl1","lvl2","lvl3"])
df["Value_x"] = np.where(df["Value_y"].notnull(), df["Value_y"], df["Value_x"])
# df.drop(["Value_y", "Value2"], axis=1, inplace=True)

答案 1 :(得分:0)

您可以尝试像这样替换索引匹配中的值:

for ind in df2.index:
    df1.loc[ind, 'Value'] = df2.loc[ind, 'Value']

如果您要替换行:

for ind in df2.index:
    df1.loc[ind,] = df2.loc[ind,]