2016-11-30 40 views
0

因此,當密鑰位於與[值相對應的範圍內時,我想用字典中的字符串替換Dataframe列中的值容忍度,價值+容忍度]。熊貓:當密鑰與公差匹配時,通過字典值更改數據幀列中的值

例如用dictionnary這樣:

dict = {101.2:"A", 110:"A", 150:"B", 170:"C", 600:"A"} 

我想替換在列「AA」的值,如果在一個範圍內,該值或多或少的公差相匹配字典中的一個鍵相應的字符串值。

如下面我想在「AA」列以匹配字典中的關鍵101.2,如果我的公差值設置爲0.5的值101的示例從而改變了值101爲「A」的匹配列的行「 AA」。

下面的例子:從剪貼板

 end start diff aa 
0 200  99 101 101 
1 250  99 151 151 
2 270  99 170.2 170.2 
3 300  99 201 201 
4 450  99 351 351 
5 600  99 501 501 
6 800  99 701 701 
7 250 200 50 50 
8 270 200 70 70 
9 300 200 100 100 
10 450 200 250 250 
11 600 200 400 400 
12 800 200 600 600 
13 270 250 20 20 
14 300 250 50 50 
15 450 250 200 200 
16 600 250 350 350 
17 800 250 550 550 
18 300 270 30 30 
19 450 270 180 180 
20 600 270 330 330 
21 800 270 530 530 
22 450 300 150 150 
23 600 300 300 300 
24 800 300 500 500 
25 600 450 150 150 
26 800 450 350 350 
27 800 600 200 200 

負載數據幀

table = pd.read_clipboard().iloc[1:] 
tol_value = 0.5 

預期的結果:

 end start diff aa 
0 200  99 101 A 
1 250  99 151 151 
2 270  99 170.2 C 
3 300  99 201 201 
4 450  99 351 351 
5 600  99 501 501 
6 800  99 701 701 
7 250 200 50 50 
8 270 200 70 70 
9 300 200 100 100 
10 450 200 250 250 
11 600 200 400 400 
12 800 200 600 600 
13 270 250 20 20 
14 300 250 50 50 
15 450 250 200 200 
16 600 250 350 350 
17 800 250 550 550 
18 300 270 30 30 
19 450 270 180 180 
20 600 270 330 330 
21 800 270 530 530 
22 450 300 150 B 
23 600 300 300 300 
24 800 300 500 500 
25 600 450 150 B 
26 800 450 350 350 
27 800 600 200 200 

我知道如何用一個確切的匹配替換:

table2 = table.replace({"aa": dict}) 

但我不知道如何使寬容一致。

我曾嘗試:

for index, row in table.iterrows(): 
    for key, value in dict.iteritems(): 
     if (row['aa']-tol_value <= key) & (key <= row['aa']+tol_value): 
      table.replace(row.aa, value) 

它的工作原理,但不幸的是這將創建一個多dataframes因爲有反覆。

任何幫助,將不勝感激

回答

1

供您參考:

創建一個新的字典來存儲您參考字典

dict = {101.2: 'A', 110: 'A', 150: 'B', 170: 'C', 600: 'A'} 

def new_dict(dict): 
    new_dict={} 
    for i,j in dict.items(): 
     if j not in new_dict.keys(): 
      new_dict[j] = [i] 
     else: 
      new_dict[j].append(i) 
    return new_dict 

new = new_dict(dict) 

{'A ':[600,101.2,110],'B':[150],'C':[170]}

飛度與新的字典數據

def fit(x, dict, tol): 
    for key in dict.keys(): 
     for item in dict[key]: # There are many items in a key 
      if (x > item-tol) and (x <= item+tol): 
       return key 
    return x 

應用功能,以您的數據

table.aa = table.aa.apply(lambda x: fit(x, new, 0.5)) 

得到它

end start diff aa 
0 200 99 101.0 A 
1 250 99 151.0 151 
2 270 99 170.2 C 
3 300 99 201.0 201 
4 450 99 351.0 351 
5 600 99 501.0 501 
6 800 99 701.0 701 
7 250 200 50.0 50 
8 270 200 70.0 70 
9 300 200 100.0 100 
10 450 200 250.0 250 
11 600 200 400.0 400 
12 800 200 600.0 A 
13 270 250 20.0 20 
14 300 250 50.0 50 
15 450 250 200.0 200 
16 600 250 350.0 350 
17 800 250 550.0 550 
18 300 270 30.0 30 
19 450 270 180.0 180 
20 600 270 330.0 330 
21 800 270 530.0 530 
22 450 300 150.0 B 
23 600 300 300.0 300 
24 800 300 500.0 500 
25 600 450 150.0 B 
26 800 450 350.0 350 
27 800 600 200.0 200 
+0

非常優雅的做法,非常感謝! –