简体   繁体   English

修改字典中的值并返回键和修改后的值

[英]Modify the values in a dictionary and return the key and the modified value

I created a dictionary with pandas and I'm trying to get only the value 我用熊猫创建了一个字典,我试图只获取值

a                   b
hello_friend        HELLO<by>
hi_friend           HI<byby>
good_friend         GOOD<bybyby>

I would like to get the list of values, apply multiple methods only on it and at the end return the key and the modified values 我想获取值列表,仅对其应用多个方法,最后返回键和修改后的值

def open_pandas():
    df = pandas.read_csv('table.csv', encoding = 'utf-8')
    dico = df.groupby('a')['b'].apply(list).to_dict()

    return dico

def methods_values(dico)

    removes = b.str.replace(r'<.*>', '')
    b_lower = removes.astype(str).str.lower()
    b_list = dico.to_dict('b')
    #here, I'm going to apply a clustering on the values

    return dico_with_modified_values

I need the two functions (but my second function is not working) and my desired output: 我需要两个函数(但是第二个函数不起作用)和所需的输出:

{"hello_friend": ['hello'],"hi_friend": ['hi'], "good_friend": ['good']}

Is this possible? 这可能吗?

I think need first processes column b of DataFrame and then convert it to dictionary of lists: 我认为首先需要处理DataFrame b列,然后将其转换为列表字典:

df = pandas.read_csv('table.csv', encoding = 'utf-8')
df['b'] = df['b'].str.replace(r'<.*>', '').str.lower()
dico = df.groupby('a')['b'].apply(list).to_dict()
print (dico)

{'good_friend': ['good'], 'hello_friend': ['hello'], 'hi_friend': ['hi']}

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

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