python-如何从excel中获取字典的单值多键
我有一张excel表格,即python-如何从excel中获取字典的单值多键,python,pandas,dictionary,Python,Pandas,Dictionary,我有一张excel表格,即 0 first |second |third |root 1 credible |credulous |credibility|cred 2 cryptogram|cryptology|cryptic |crypt 3 aquatic |aquarium |aqueduct |aqua 我想从上述excel表中导入字典的键和值: 例如: 我已经编写了一个用于存储这些值的字典代码 new_dic
0 first |second |third |root
1 credible |credulous |credibility|cred
2 cryptogram|cryptology|cryptic |crypt
3 aquatic |aquarium |aqueduct |aqua
我想从上述excel表中导入字典的键和值:
例如: 我已经编写了一个用于存储这些值的字典代码
new_dict = {
('credible','credulous','credibility') : 'cred',
('cryptogram','cryptology','cryptic') : 'crypt'
}
因此,我不想为字典中的每个单词编写关键字和值,而是想从excel导入,其中前3列(即第一列、第二列、第三列)应该是关键字,最后一列(即根)将是字典的值。有可能吗?
对不起我的英语。
谢谢用于为除
根以外的每一列(本例中为前三列)设置索引,然后调用:
由前3列使用,包括:
请发布您编写的代码,这可能会有所帮助
df.set_index(df.columns.difference(['root']).tolist()).root.to_dict()
{
('aquatic', 'aquarium', 'aqueduct') : 'aqua',
('credible', 'credulous', 'credibility') : 'cred',
('cryptogram', 'cryptology', 'cryptic') : 'crypt'
}
d = df.set_index(['first','second','third'])['root'].to_dict()
print (d)
{('credible', 'credulous', 'credibility'): 'cred',
('aquatic', 'aquarium', 'aqueduct'): 'aqua',
('cryptogram', 'cryptology', 'cryptic'): 'crypt'}