我有一个像这样的pandas数据框:
a b c
foo bar baz
bar foo baz
foobar barfoo baz
我在python中定义了以下函数:
def somefunction (row):
if row['a'] == 'foo' and row['b'] == 'bar':
return 'yes'
return 'no'
它完美无缺。但我需要对if
函数进行一些小调整,以考虑partial string
次匹配。
我尝试了几种组合,但我似乎无法让它发挥作用。我收到以下错误:
("'str' object has no attribute 'str'", 'occurred at index 0')
Iv尝试的功能是:
def somenewfunction (row):
if row['a'].str.contains('foo')==True and row['b'] == 'bar':
return 'yes'
return 'no'
答案 0 :(得分:1)
使用contains
表示布尔掩码,然后使用numpy.where
:
m = df['a'].str.contains('foo') & (df['b'] == 'bar')
print (m)
0 True
1 False
2 False
dtype: bool
df['new'] = np.where(m, 'yes', 'no')
print (df)
a b c new
0 foo bar baz yes
1 bar foo baz no
2 foobar barfoo baz no
或者,如果需要检查列b
以查找子字符串:
m = df['a'].str.contains('foo') & df['b'].str.contains('bar')
df['new'] = np.where(m, 'yes', 'no')
print (df)
a b c new
0 foo bar baz yes
1 bar foo baz no
2 foobar barfoo baz yes
如果需要自定义功能,较大的DataFrame
:
def somefunction (row):
if 'foo' in row['a'] and row['b'] == 'bar':
return 'yes'
return 'no'
print (df.apply(somefunction, axis=1))
0 yes
1 no
2 no
dtype: object
def somefunction (row):
if 'foo' in row['a'] and 'bar' in row['b']:
return 'yes'
return 'no'
print (df.apply(somefunction, axis=1))
0 yes
1 no
2 yes
dtype: object
<强>计时强>:
df = pd.concat([df]*1000).reset_index(drop=True)
def somefunction (row):
if 'foo' in row['a'] and row['b'] == 'bar':
return 'yes'
return 'no'
In [269]: %timeit df['new'] = df.apply(somefunction, axis=1)
10 loops, best of 3: 60.7 ms per loop
In [270]: %timeit df['new1'] = np.where(df['a'].str.contains('foo') & (df['b'] == 'bar'), 'yes', 'no')
100 loops, best of 3: 3.25 ms per loop
df = pd.concat([df]*10000).reset_index(drop=True)
def somefunction (row):
if 'foo' in row['a'] and row['b'] == 'bar':
return 'yes'
return 'no'
In [272]: %timeit df['new'] = df.apply(somefunction, axis=1)
1 loop, best of 3: 614 ms per loop
In [273]: %timeit df['new1'] = np.where(df['a'].str.contains('foo') & (df['b'] == 'bar'), 'yes', 'no')
10 loops, best of 3: 23.5 ms per loop
答案 1 :(得分:1)
你的例外可能来自你写的事实
if row['a'].str.contains('foo')==True
删除&#39; .str&#39;:
if row['a'].contains('foo')==True