根据两个字符串值评估 Pandas DataFrame
Evaluate Pandas DataFrame against two string values
我有一个 Pandas DataFrame "table",其中包含一个名为 "OPINION" 的列,其中填充了字符串值。我想创建一个名为 "cond5" 的新列,其中 "OPINION" 是 "buy" 或 "neutral".
的每一行都用 TRUE 填充
我试过了
table["cond5"]= table.OPINION == "buy" or table.OPINION == "neutral"
这给了我一个错误,
table["cond5"]= table.OPINION.all() in ("buy", "neutral")
所有行的 returns FALSE。
正如 Ed Chum 指出的那样,您可以使用 isin
method:
table['cond5'] = table['OPINION'].isin(['buy', 'neutral'])
isin
检查是否完全相等。也许那将是 最简单和最易读的 。
修复
table["cond5"] = table.OPINION == "buy" or table.OPINION == "neutral"
使用
table["cond5"] = (table['OPINION'] == "buy") | (table['OPINION'] == "neutral")
括号是必需的,因为 |
比 ==
多 higher precedence (binding power)。
x or y
要求 x
和 y
为布尔值。
(table['OPINION'] == "buy") or (table['OPINION'] == "neutral")
引发错误,因为 Series can no be reduced to a single boolean value。
因此,请改用逻辑或运算符 |
,它按元素取 Series 中值的 or
。
另一种选择是
import numpy as np
table["cond5"] = np.logical_or.reduce([(table['OPINION'] == val) for val in ('buy', 'neutral')])
如果 ('buy', 'neutral')
是一个更长的元组,这可能会有用。
另一种选择是使用 Pandas' vectorized string method, str.contains
:
table["cond5"] = table['OPINION'].str.contains(r'buy|neutral')
str.contains
在 Cythonized 循环中对 table['OPINION']
中的每个项目执行模式 r'buy|neutral'
的正则表达式搜索。
现在如何决定使用哪一个?这是使用 IPython:
的时间基准
In [10]: table = pd.DataFrame({'OPINION':np.random.choice(['buy','neutral','sell',''], size=10**6)})
In [11]: %timeit (table['OPINION'] == "buy") | (table['OPINION'] == "neutral")
10 loops, best of 3: 121 ms per loop
In [12]: %timeit np.logical_or.reduce([(table['OPINION'] == val) for val in ('buy', 'neutral')])
1 loops, best of 3: 204 ms per loop
In [13]: %timeit table['OPINION'].str.contains(r'buy|neutral')
1 loops, best of 3: 474 ms per loop
In [14]: %timeit table['OPINION'].isin(['buy', 'neutral'])
10 loops, best of 3: 40 ms per loop
看起来 isin
最快.
我有一个 Pandas DataFrame "table",其中包含一个名为 "OPINION" 的列,其中填充了字符串值。我想创建一个名为 "cond5" 的新列,其中 "OPINION" 是 "buy" 或 "neutral".
的每一行都用 TRUE 填充我试过了
table["cond5"]= table.OPINION == "buy" or table.OPINION == "neutral"
这给了我一个错误,
table["cond5"]= table.OPINION.all() in ("buy", "neutral")
所有行的 returns FALSE。
正如 Ed Chum 指出的那样,您可以使用 isin
method:
table['cond5'] = table['OPINION'].isin(['buy', 'neutral'])
isin
检查是否完全相等。也许那将是 最简单和最易读的 。
修复
table["cond5"] = table.OPINION == "buy" or table.OPINION == "neutral"
使用
table["cond5"] = (table['OPINION'] == "buy") | (table['OPINION'] == "neutral")
括号是必需的,因为 |
比 ==
多 higher precedence (binding power)。
x or y
要求 x
和 y
为布尔值。
(table['OPINION'] == "buy") or (table['OPINION'] == "neutral")
引发错误,因为 Series can no be reduced to a single boolean value。
因此,请改用逻辑或运算符 |
,它按元素取 Series 中值的 or
。
另一种选择是
import numpy as np
table["cond5"] = np.logical_or.reduce([(table['OPINION'] == val) for val in ('buy', 'neutral')])
如果 ('buy', 'neutral')
是一个更长的元组,这可能会有用。
另一种选择是使用 Pandas' vectorized string method, str.contains
:
table["cond5"] = table['OPINION'].str.contains(r'buy|neutral')
str.contains
在 Cythonized 循环中对 table['OPINION']
中的每个项目执行模式 r'buy|neutral'
的正则表达式搜索。
现在如何决定使用哪一个?这是使用 IPython:
的时间基准In [10]: table = pd.DataFrame({'OPINION':np.random.choice(['buy','neutral','sell',''], size=10**6)})
In [11]: %timeit (table['OPINION'] == "buy") | (table['OPINION'] == "neutral")
10 loops, best of 3: 121 ms per loop
In [12]: %timeit np.logical_or.reduce([(table['OPINION'] == val) for val in ('buy', 'neutral')])
1 loops, best of 3: 204 ms per loop
In [13]: %timeit table['OPINION'].str.contains(r'buy|neutral')
1 loops, best of 3: 474 ms per loop
In [14]: %timeit table['OPINION'].isin(['buy', 'neutral'])
10 loops, best of 3: 40 ms per loop
看起来 isin
最快.