简体   繁体   English

根据字典在数据框中创建新列

[英]Create a new column in data frame from a dict

I have a Pandas df: 我有一个熊猫df:

     a   b  c
0   'k'  2  4
1   'l'  3  7
2   'm'  0  -3
3   'n'  4  4

I have a dict: {'k': 'kilo', 'l': 'lima', 'm': 'mike', 'n': 'november'} 我有一个字典:{'k':'kilo','l':'lima','m':'mike','n':'十一月'}

How can I create a new column in my df across those keys from the dict: 如何在df中跨字典的那些键创建新列:

     a   b  c    new
0   'k'  2  4   'kilo'
1   'l'  3  7   'lima'
2   'm'  0  -3  'mike'
3   'n'  4  4   'november'

Thank you. 谢谢。

Just call map and pass the dict, this will perform a lookup of your series values against the values in your dict, this is vectorised and will be much faster than doing this in a loop: 只需调用map并传递dict,这将对您的dict中的值执行一系列值的查找,这是矢量化的,并且比循环执行要快得多:

In [26]:

t = {'k': 'kilo', 'l': 'lima', 'm': 'mike', 'n': 'november'}
df['new'] = df['a'].map(t)
df
Out[26]:
   a  b  c       new
0  k  2  4      kilo
1  l  3  7      lima
2  m  0 -3      mike
3  n  4  4  november

I notice that in your data you have quote marks around your data, in which case the above won't work because your dict keys are just a single character so you would need to define your dict with quote marks also for the keys: 我注意到,在您的数据中,您的数据周围带有引号,在这种情况下,上述操作将无效,因为您的字典键只是一个字符,因此您还需要为键定义带引号的字典:

In [28]:

t = {"'k'": 'kilo', "'l'": 'lima', "'m'": 'mike', "'n'": 'november'}
df['new'] = df['a'].map(t)
df
Out[28]:
     a  b  c       new
0  'k'  2  4      kilo
1  'l'  3  7      lima
2  'm'  0 -3      mike
3  'n'  4  4  november

however, I would just remove the quote marks if they are unnecessary: 但是,如果不需要引号,我将删除它们:

In [30]:

df['a'] = df['a'].str.replace("'", '')
df['a']
Out[30]:
0    k
1    l
2    m
3    n
Name: a, dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM