Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 使用np.where()将数据帧中的真/假值更改为离散值_Python_Pandas_Contains_Assign_Extraction - Fatal编程技术网

Python 使用np.where()将数据帧中的真/假值更改为离散值

Python 使用np.where()将数据帧中的真/假值更改为离散值,python,pandas,contains,assign,extraction,Python,Pandas,Contains,Assign,Extraction,我正在尝试为大学名称列表指定一个州名称: df = pd.DataFrame({'College': pd.Series(['University of Michigan', 'University of Florida', 'Iowa State'])}) State = ['Michigan', 'Iowa'] df['State'] = np.where(df['College'].str.contains('|'.join(State)), 'state','--') 我想用状

我正在尝试为大学名称列表指定一个州名称:

df = pd.DataFrame({'College': pd.Series(['University of Michigan', 'University of Florida', 'Iowa State'])})
State = ['Michigan', 'Iowa']
df['State'] = np.where(df['College'].str.contains('|'.join(State)),
    'state','--')
我想用状态的实际名称替换匹配时出现的“state”值。例如:密歇根大学>密歇根(而不是“州”)。最终,“State”将包含所有50个州,因此我不能为每个州名编写50个“np.where”语句


谢谢你的帮助

您可以在此处使用
str.extract
,而不是
np.where

In [290]: df['State'] = df['College'].str.extract('({})'.format('|'.join(State)), expand=True)

In [291]: df
Out[291]: 
                  College     State
0  University of Michigan  Michigan
1   University of Florida       NaN
2              Iowa State      Iowa
状态=[
“华盛顿”“威斯康星州”“西弗吉尼亚州”“佛罗里达州”“怀俄明州”
“新罕布什尔州”“新泽西州”“新墨西哥州”“国家”“北卡罗来纳州”
“北达科他州”“内布拉斯加州”“纽约”“罗德岛”“内华达州”“关岛”
“科罗拉多州”“加利福尼亚州”“乔治亚州”“康涅狄格州”“俄克拉何马州”“俄亥俄州”“堪萨斯州”
“南卡罗来纳州”“肯塔基州”“俄勒冈州”“南达科他州”“特拉华州”
“哥伦比亚特区”“夏威夷”“波多黎各”“德克萨斯”“路易斯安那”
“田纳西州”“宾夕法尼亚州”“弗吉尼亚州”“维尔京群岛”“阿拉斯加”“阿拉巴马州”
“美属萨摩亚”“阿肯色州”“佛蒙特州”“伊利诺伊州”“印第安纳州”“爱荷华州”
“亚利桑那州”“爱达荷州”“缅因州”“马里兰州”“马萨诸塞州”“犹他州”“密苏里州”
“明尼苏达州”“密歇根州”“蒙大拿州”“北马里亚纳群岛”“密西西比州”
]
state_str='|'。连接(States)
update(df.College.str.extract(r'(?P{})').format(state_str,expand=True))
df

States = [
            'Washington' 'Wisconsin' 'West Virginia' 'Florida' 'Wyoming'
            'New Hampshire' 'New Jersey' 'New Mexico' 'National' 'North Carolina'
            'North Dakota' 'Nebraska' 'New York' 'Rhode Island' 'Nevada' 'Guam'
            'Colorado' 'California' 'Georgia' 'Connecticut' 'Oklahoma' 'Ohio' 'Kansas'
            'South Carolina' 'Kentucky' 'Oregon' 'South Dakota' 'Delaware'
            'District of Columbia' 'Hawaii' 'Puerto Rico' 'Texas' 'Louisiana'
            'Tennessee' 'Pennsylvania' 'Virginia' 'Virgin Islands' 'Alaska' 'Alabama'
            'American Samoa' 'Arkansas' 'Vermont' 'Illinois' 'Indiana' 'Iowa'
            'Arizona' 'Idaho' 'Maine' 'Maryland' 'Massachusetts' 'Utah' 'Missouri'
            'Minnesota' 'Michigan' 'Montana' 'Northern Mariana Islands' 'Mississippi'
]

state_str = '|'.join(States)
df.update(df.College.str.extract(r'(?P<State>{})'.format(state_str), expand=True))

df