我有这样的列表和字典:
list_1 = ['if looks kill then i'm murdering]
dic_1 = {"kill": -2, "murdering": -3}
Run Code Online (Sandbox Code Playgroud)
我想提取与字典键匹配的列表项并将其附加到集合中.我有两个问题:1.我无法提取与字典中的键匹配的列表项2.如何将列表项追加到集中?
set_1 = set()
for items in list_1:
list_1 = items.lower().split()
for term in dic_1:
forth_list = [words for words in list_1 if term != words]
print forth_list
Run Code Online (Sandbox Code Playgroud)
['if', 'looks', 'then', 'i', 'm', 'murdering']
['if', 'looks', 'kill', 'then', 'i', 'm']
set_1.add(forth_list) # this produce a TypeError: unhashable type: 'list'
print set_1
Run Code Online (Sandbox Code Playgroud) 我有一个pandas DataFrame:
from pandas import DataFrame
import pandas as pd
df2 = DataFrame({'a' : ['one', 'one', 'two','two', 'three', 'two', 'one', 'six'],
'b' : ['x', 'y', 'z', 'y', 'x', 'y', 'x', 'x']})
Run Code Online (Sandbox Code Playgroud)
我需要使用列对其进行分组'a'.
df3 = df2.groupby(['a'])
Run Code Online (Sandbox Code Playgroud)
接下来,我想将列'b'转换为逗号分隔的字符串,结果表应如下所示:
a b
---------------
one j, k, l
two m, n, o
three p, q
Run Code Online (Sandbox Code Playgroud)
有没有人知道怎么做而不留下熊猫?看起来很简单,但找不到在熊猫里面做的方法.