Создать словарь из двух numpy.ndarray? - PullRequest
0 голосов
/ 29 ноября 2018

У меня есть два numpy.ndarray

[[' THE OLD TESTAMENT ']
[' SEAN SONG ']
[' CITY WALK ']]

и

[[' This is the name of an Old Testament ']
 [' Hello this is great ']
[' Wait the way you are doing ']]

Я хочу преобразовать эти ndarray в словарь.

 {
"THE OLD TESTAMENT": "This is the name of an Old Testament",
"SEAN SONG": "Hello this is great",
"CITY WALK": Wait the way you are doing 
 }

Я используюкод написан ниже

keys = df.as_matrix()
print (keys)
values = df1.as_matrix()
print (values)
new_dict = dict(izip(keys, values))

Ответы [ 4 ]

0 голосов
/ 29 ноября 2018

Сначала сожмите свои массивы:

In [1]: import numpy as np

In [2]: keys = np.array(
   ...:     [[' THE OLD TESTAMENT '],
   ...:     [' SEAN SONG '],
   ...:     [' CITY WALK ']]
   ...: )

In [3]: values = np.array(
   ...:     [[' This is the name of an Old Testament '],
   ...:      [' Hello this is great '],
   ...:     [' Wait the way you are doing ']]
   ...: )

In [4]: dict(zip(keys.squeeze(), values.squeeze()))
Out[4]:
{' CITY WALK ': ' Wait the way you are doing ',
 ' SEAN SONG ': ' Hello this is great ',
 ' THE OLD TESTAMENT ': ' This is the name of an Old Testament '}

Или просто используйте нарезку:

In [5]: dict(zip(keys[:,0], values[:,0]))
Out[5]:
{' CITY WALK ': ' Wait the way you are doing ',
 ' SEAN SONG ': ' Hello this is great ',
 ' THE OLD TESTAMENT ': ' This is the name of an Old Testament '}
0 голосов
/ 29 ноября 2018

Преобразование в arrays не требуется, используйте iloc для выбора первого столбца с zip:

new_dict = dict(zip(df.iloc[:, 0], df1.iloc[:, 0]))

Или выберите столбцы по именам:

new_dict = dict(zip(df['col'], df1['col']))
0 голосов
/ 29 ноября 2018
keys = [[' THE OLD TESTAMENT '],[' SEAN SONG '],[' CITY WALK ']]

values = [[' This is the name of an Old Testament '], [' Hello this is great '],[' Wait the way you are doing ']]


keys = [x[0] for x in keys]

values = [x[0] for x in values]


dictionary = dict(zip(keys, values))
0 голосов
/ 29 ноября 2018
keyh=[[' THE OLD TESTAMENT '],
[' SEAN SONG '],
[' CITY WALK ']]

valueh=[[' This is the name of an Old Testament '],
 [' Hello this is great '],
[' Wait the way you are doing ']]

dictionary = dict(zip([item[0] for item in keyh], [item[0] for item in valueh]))

Выход

{
"THE OLD TESTAMENT": "This is the name of an Old Testament",
"SEAN SONG": "Hello this is great",
"CITY WALK": "Wait the way you are doing"
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...