簡體   English   中英

修改字典中的值並返回鍵和修改后的值

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

我用熊貓創建了一個字典,我試圖只獲取值

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

我想獲取值列表,僅對其應用多個方法,最后返回鍵和修改后的值

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

我需要兩個函數(但是第二個函數不起作用)和所需的輸出:

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

這可能嗎?

我認為首先需要處理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