Saturday, 15 May 2010

python - no item named False error when filtering DataFrame -



python - no item named False error when filtering DataFrame -

i trying subset dataframe using in operator. in 'data' column, of strings contain text looking 'go'. however, error get:

in: subset = df['go' in df.data] out: keyerror: u'no item named false'

can help this? have tried parentheses around boolean statement.

use str.contains method test if go in each string in data column -- returns boolean array can used select rows of df:

df.loc[df['data'].str.contains(r'go')]

for example,

in [74]: df = pd.dataframe({'data': ['no', 'go', 'bogo']}) in [75]: df['data'].str.contains(r'go') out[75]: 0 false 1 true 2 true name: data, dtype: bool in [76]: df.loc[df['data'].str.contains(r'go')] out[76]: info 1 go 2 bogo

'go' in df['data'] testing if go in index of df['data'] , returning single boolean value, false. df[false] raising keyerror because there no column named false.

python pandas

No comments:

Post a Comment