[英]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.