Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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:如何从pandas系列的字典中获取值_Python_Dictionary_Pandas_Key_Dataframe - Fatal编程技术网

Python:如何从pandas系列的字典中获取值

Python:如何从pandas系列的字典中获取值,python,dictionary,pandas,key,dataframe,Python,Dictionary,Pandas,Key,Dataframe,我对python非常陌生,并尝试从字典中获取值,其中键是在dataframe列(dataframe列)中定义的。我搜索了很多,最近的是一个 下面的链接中有一个问题,但没有答案 所以,在这里,我试图为同样类型的问题找到答案 我有一本字典 type_dict = {3: 'foo', 4:'bar',5:'foobar', 6:'foobarbar'} 以及具有以下列的数据框: >>> df.type 0 3 1 4 2 5 3 6 4

我对python非常陌生,并尝试从字典中获取值,其中键是在dataframe列(dataframe列)中定义的。我搜索了很多,最近的是一个 下面的链接中有一个问题,但没有答案

所以,在这里,我试图为同样类型的问题找到答案

我有一本字典

type_dict = {3: 'foo', 4:'bar',5:'foobar', 6:'foobarbar'}
以及具有以下列的数据框:

>>> df.type
0     3
1     4
2     5
3     6
4     3
5     4
6     5
7     6
8     3
我想创建一个新列,其中包含相应的type_dict值,但我唯一能想到的是以下内容,但它不起作用:

type_dict[df.type]
TypeError:“Series”对象是可变的,因此无法对其进行散列

type_dict[df.type.values]
TypeError:不可损坏的类型:“numpy.ndarray”

更新问题:

对于熊猫数据帧,比如'df',我如何用类型作为标记字典的键来绘制米的速度

mkr_dict = {'gps': 'x', 'phone': '+', 'car': 'o'}

x = {'speed': [10, 15, 20, 18, 19], 'meters' : [122, 150, 190, 230, 300], 'type': ['phone', 'phone', 'gps', 'gps', 'car']}

df = pd.DataFrame(x)
   meters  speed   type
0     122     10  phone
1     150     15  phone
2     190     20    gps
3     230     18    gps
4     300     19    car

plt.scatter(df.meters, df.Speed, marker = df.type.map(mkr_dict)) 

散点图对我不起作用…

在熊猫中,这应该起作用

df['val'] = df.apply(lambda x: type_dict[x['type']], axis=1)

将dict作为参数传递给:


这将查找dict中的键值,并从dict返回相关值。

AFAIK,普通python没有“数据帧”的概念。如果您使用的是框架或库(如),请指出是哪一个。我认为您的分散问题应该是一个新问题,我不是matplotlib专家,但我认为您可以使用以下答案实现:基本上,这会在每一行上迭代调用分散并传递x、y坐标和标记样式谢谢!!!!!!因此,它确实从字典中创建了一个值列表。但是我试图在散点图中使用它作为标记,但失败了,错误是:TypeError:“Series”对象是可变的,因此它们不能被散列到plt.scatter(output.x,output.y,s=area,marker=output.DataPointType.map(mkr_dict))。有什么想法吗?你必须解释你是如何传递这些值的,但是列表和序列是可变的,一般来说,尽管大多数这些绘图库都与iterable容器(如numpy数组)接口,并且应该与pandas DataFrames兼容。我更新了我的问题,你能再看一看吗?谢谢
In [79]:

df['type'].map(type_dict)
Out[79]:
0          foo
1          bar
2       foobar
3    foobarbar
4          foo
5          bar
6       foobar
7    foobarbar
8          foo
Name: type, dtype: object