python - 创建两个 numpy.ndarray 的字典?

标签 python pandas list numpy dictionary

我有两个 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))

最佳答案

无需转换为数组,请使用iloc使用 zip 选择第一列:

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

或按名称选择列:

new_dict = dict(zip(df['col'], df1['col']))

关于python - 创建两个 numpy.ndarray 的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533819/

相关文章:

list - 在 Groovy 的多重赋值中获取列表的尾部

python - 转换为 int64 时,DataFrame 中的混合时间戳会导致错误

python - 向 pandas DataFrame 添加多个空列

python - Django 中的 get_or_create 函数如何返回两个值?

python - 管道内的列转换问题

python - Pandas:无需循环即可获取数据子集

jQuery 复选框值以逗号分隔列表

python - 在Python中从文本文件的几个相同结构的 block 中解析数据

python - python 列表中每个唯一元素的所有索引

Python pandas 智能追加