import pandas as pd
data = {'state':['Ohio','Ohio','Merry','Nevaio','Nevada'],
'year':[2000,2001,2002,2001,2002],
'pop':[1.5,1.7,3.6,2.4,2.9]
}
frame = pd.DataFrame(data,columns=['year','state','pop','debt'],index=['one','two','three','four','five'])
frame
数据如上图所示,现找出state中'state'列中含有字符'io'的行,将其的debt列赋值为'aa',实现代码如下:
frame.ix[frame['state'].str.contains('io'),['debt']] = 'aa'
frame
现找出state中'state'列中等于‘Ohio’行,将其的debt列赋值为'aa',frame还是开头定义的,实现代码如下:
frame.ix[frame['state']=='Ohio',['debt']] = 'aa'
frame
多个选择条件时,用(‘&’、‘|’),如现找出state中'state'列中等于‘Ohio’行或者‘Merry’的行,用'|',将其的debt列赋值为'aa',实现代码如下:
frame.ix[(frame['state']=='Ohio')|(frame['state']=='Merry'),['debt']] = 'aa'
frame