【发布时间】:2018-07-17 00:44:54
【问题描述】:
我的数据框是这样的
df = pd.Dataframe({ 'a': ["10001", "10001", "10002", "10002" , "10002"], 'b': ['hello', 'hello', 'hola', 'hello', 'hola']})
我想创建一个新的布尔值列“c”,条件如下:
- 如果 'a' 的值相同(即第 1 行和第 2 行,第 3 和第 4 和第 5 行),请检查这些行的 'b' 值是否相同。 (第 2 行返回 True。第 4 行返回 False)。
- 如果 'a' 的值不同,请跳过。
我当前的代码如下:
def check_consistency(col1,col2):
df['match'] = df[col1].eq(df[col1].shift())
t = []
for i in df['match']:
if i == True:
t.append(df[col2].eq(df[col2].shift()))
check_consistency('a','b')
它返回错误。
【问题讨论】:
-
请为您提供的示例显示您想要的输出。
标签: python pandas dataframe string-comparison