我想要一列"PRI/TEN"


当PRIMARY RESOURCE列包含"BERIX"时,PRI/TEN应该
"TBC"
当PRIMARY RESOURCE列包含"BERIXTVX"时,
PRI/TEN应该具有"TEN"
当PRIMARY RESOURCE列包含
"BERIXVX"PRI/TEN应该具有"PRI"


我试过了:

data1['PRI/ TEN'] = np.where(data1['First']!='BERIXVX', 'PRI','TEN')


这是我的原始专栏

Primary Resource
BERIX_MYANMAR ONLY
BERIXTVX_BOOST IPV
BERIXTVX_BOOST IPV
BERIXVX_ENCEPUR A AD
BERIXVX_ENCEPUR AD
BERIXTVX_BOOST IPV
BERIXTVX_BEXSERO


这就是我要的

Primary Resource    PRI/TEN
BERIX_MYANMAR ONLY  TBC
BERIXTVX_BOOST IPV  TEN
BERIXTVX_BOOST IPV  TEN
BERIXVX_ENCEPUR A AD    PRI
BERIXVX_ENCEPUR AD  PRI
BERIXTVX_BOOST IPV  TEN
BERIXTVX_BEXSERO    TEN

最佳答案

尝试这个:

def categorizer(row):
    if 'BERIXTVX' in row:
        return 'TEN'
    elif 'BERIXVX' in row:
        return 'PRI'
    elif 'BERIX' in row:
        return 'TBC'

    # Return a default value
    return None


data1['PRI/TEN'] = data1['PRIMARY RESOURCE'].apply(categorizer)


这会将函数categorizer应用于PRIMARY RESOURCE列中的每一行。它不是矢量化的,因此如果遇到性能问题,请告诉我。

08-24 15:35