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

时间:2018-08-09 04:00:49

标签: python python-3.x pandas

我用熊猫创建了一个字典,我试图只获取值

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

这可能吗?

2 个答案:

答案 0 :(得分:1)

我认为需要先处理b的列DataFrame,然后将其转换为列表字典:

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

答案 1 :(得分:0)

IIUC

public class NewSomeGroup
{
    public string ReferenceId { get; set; }
    public List<Some> sbnotificationlist { get; set; }
}

public class Some
{
    public string Title { get; set; }
    public string projectid { get; set; }
    public SimpleAccountInfo AccountInfo { get; set; }
    public string Type { get; set; } 
    public string ReferenceId { get; set; }
    public string iconPath { get; set; }
    public DateTime CreateOnUtc { get; set; }
}

输出

df['b'] = df.b.str.replace(r'<.*>', '').str.lower().transform(lambda k: [k])
df.set_index('a').to_dict()['b']