简体   繁体   中英

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:

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']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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