Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字典中一个键的多个值_Python_Pandas - Fatal编程技术网

Python 字典中一个键的多个值

Python 字典中一个键的多个值,python,pandas,Python,Pandas,我有一个数据框架get_表,有两列 get_tables= df[['Database Schema', 'Database Table']] get_tables = get_tables.drop_duplicates(subset=None, keep='first', inplace=False) print(get_tables) dict_get_tables= dict(zip(get_tables['Database Schema'], get_tables['Database

我有一个数据框架get_表,有两列

get_tables= df[['Database Schema', 'Database Table']]
get_tables = get_tables.drop_duplicates(subset=None, keep='first', inplace=False)
print(get_tables)
dict_get_tables= dict(zip(get_tables['Database Schema'], get_tables['Database Table']))
print(dict_get_tables)
get_tables将输出打印为

Database Schema Database Table
0             abc       customer
3             abc           cust
4             def        Student
dict_get_tables将输出打印为

{'abc': 'cust', 'def': 'Student'}
我的要求是,当我做像
dict\u get\u tables.get('abc')
这样的dict\u get\u表时,我必须同时获得客户和客户
dict_get_tables.get('def')
应该给我
Student
你所需要的就是为每个键保留一个列表:`

dict_table= dict() 

for l in list:
    if l[0] in dict_table:
        # append the new number to the existing array at this slot
        dict_table[l[0]].append(l[1])
    else:
        # create a new array in this slot
        dict_table[l[0]] = [l[1]]

您可以在上面的代码中使用这样的字符串。

如果希望字符串通过分隔符连接,请使用
groupby
with
apply
join

out1 = get_tables.groupby('Database Schema')['Database Table'].apply(', '.join).to_dict()
print (out1)
{'abc': 'customer, cust', 'def': 'Student'}

print (out1['abc'])
customer, cust

print (out1['def'])
Student
如果需要列表,请使用
groupby
list

out2 = get_tables.groupby('Database Schema')['Database Table'].apply(list).to_dict()
print (out2)
{'abc': ['customer', 'cust'], 'def': ['Student']}

print (out2['abc'])
['customer', 'cust']

print (out2['def'])
['Student']
Anf如果需要组合-一个元素的标量和多个值的列表:

out3 = (get_tables.groupby('Database Schema')['Database Table']
                  .apply(lambda x: x.tolist() if len(x) > 1 else x.iat[0])
                  .to_dict())
print (out3)
{'abc': ['customer', 'cust'], 'def': 'Student'}

print (out3['abc'])
['customer', 'cust']

print (out3['def'])
Student

dict不允许复制密钥。因此,要处理dict中的重复键,需要使用
list
作为值。您可以使用
groupby.unique
创建列表的
dict
,如下所示:

dict_get_tables = df.groupby('Database Schema')['Database Table'].unique().to_dict()

Out[656]:
{'abc': array(['customer', 'cust'], dtype=object),
 'def': array(['Student'], dtype=object)}

dict_get_tables.get('abc')
Out[660]: array(['customer', 'cust'], dtype=object)

dict_get_tables.get('def')
Out[661]: array(['Student'], dtype=object)

使用groupby和list的方法奏效了,但现在当我尝试tables=out2(schema)时,schema基本上有值'abc'和'def',我得到了错误TypeError:'dict'对象不是callable@Harshi-hmmm,很难知道,可能需要
tables=out2(str(schema))
?tables=out2(str(schema))不起作用,但是谢谢你的回答一个想法-你认为
[]
而不是
()
tables=out2[schema]