简体   繁体   English

比较字典键值和多值索引

[英]Compare dictionary key value to multi-value index

I want to compare the following dictionary key: 我想比较以下字典键:

{('F123', 1): 'R'}

To the index of the following dataframe ('F123', 1): 指向以下数据帧的索引(“ F123”,1):

Connector Pin   Connector   Pin          Adj.         Color        
 F123      1        F123    1         [2, 6, 7]        NaN

If the dictionary key is equal to the dataframe index ('F123', 1) I want copy the dictionary value ('R') into the color column associated with the matching index. 如果字典键等于数据帧索引('F123',1),我想将字典值('R')复制到与匹配索引关联的颜色列中。 Both the dictionary and dataframe have a number of rows but for explanations sake I included only one of each. 字典和数据框都具有许多行,但是出于解释的原因,我仅包括其中每一行。 Speed doesn't matter as the data set is not big enough to matter. 速度无关紧要,因为数据集不够重要。

if(df.index == dict.key()):
    df['Color'] = dict.value()

I am struggling syntactically on how to approach this problem. 我在语法上努力解决该问题。

update: I attempted this below (which I know is wrong). 更新:我尝试以下(我知道这是错误的)。 Still trying to nail down how to test all dict. 仍在尝试确定如何测试所有字典。 keys one by one without hardcoding it in. 无需硬编码就一一输入。

s = df.iterrows(pd.Series(dict.keys()))
df['Color'] = s

Make a Series from the dictionary and then assign the Color column to that: 从字典创建一个系列,然后将“颜色”列分配给该系列:

In [11]: df
Out[11]:
       Connector  Pin Connector.1 Pin.1 Adj.  Color
F123 1      F123    1         [2,    6,   7]    NaN

In [12]: s = pd.Series({('F123', 1): 'R'})

In [13]: df["Color"] = s

In [14]: df
Out[14]:
       Connector  Pin Connector.1 Pin.1 Adj. Color
F123 1      F123    1         [2,    6,   7]     R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM