Python 从np.where()获取单个值以填充新列

Python 从np.where()获取单个值以填充新列,python,pandas,numpy,Python,Pandas,Numpy,我试图获取np.where()函数的值来填充整个列,但遇到了一些问题。我有两个表,一个包含两列的查找表,“injuryType”和“Id”,其中injuryType是一个字符串,Id是一个int。第二个表是一个新表,我试图从第一个表中获取Id,但整个列的Id应该相同 #LookupTable Id InjuryType 1 acute_Shoulder 2 chronic_Shoulder 3 acute_Ankle 4 chr

我试图获取np.where()函数的值来填充整个列,但遇到了一些问题。我有两个表,一个包含两列的查找表,“injuryType”和“Id”,其中injuryType是一个字符串,Id是一个int。第二个表是一个新表,我试图从第一个表中获取Id,但整个列的Id应该相同

    #LookupTable 
    Id  InjuryType
    1   acute_Shoulder
    2   chronic_Shoulder
    3   acute_Ankle
    4   chronic_Ankle


   #The table that I want to create (new)
   Id   Description
   4    a
   4    b
   4    foo
   4    bar
我正在努力处理新df的Id列。我试图提取np.where()函数的内容来填充id列,但没有效果

 #Try setting id to Id from LookupTable where InjuryType is chronic_Ankle, essentially new['Id'] = 4
 new['Id'] = np.where(lookup['InjuryType'] == 'chronic_Ankle',lookup['Id'],'NULL')
 
 #Also tired setting the the function to a value.
 i = np.where(lookup['InjuryType'] == 'chronic_Ankle',lookup['Id'],'NULL')
 i = [i for i in i != 'NULL']  #returns ['4'], but will get index error if I try setting it to this

谢谢大家!

可能的解决方案之一:

new_df = pd.DataFrame({
    'Id': lookup[lookup.InjuryType == 'chronic_Ankle'].Id.item(),
    'Description': ['a', 'b', 'foo', 'bar']})
结果是:

   Id Description
0   4           a
1   4           b
2   4         foo
3   4         bar
另一种选择:

new_df = pd.DataFrame({
    'Id': lookup.set_index('InjuryType').loc['chronic_Ankle', 'Id'],
    'Description': ['a', 'b', 'foo', 'bar']})