我有如下所示的df:
Policy Letter Password Lower Upper Count Lower_Minus_1 Upper_Minus_1
0 4-5 l rllllj 4 5 4 3 4
1 4-10 s ssskssphrlpscsxrfsr 4 10 8 3 9
2 14-18 p ppppppppppppppppppp 14 18 19 13 17
3 1-6 z zzlzvmqbzzclrz 1 6 6 0 5
4 4-5 j jhjjhxhjkxj 4 5 5 3 4 Lower_Minus_1值将用作索引,用于搜索密码中的该位置,以查看它是否与“信函”列中的字母匹配。
这条线适用于:
print(df['Password'].str[3] == df['Letter'])但是,它根据“密码”中每一行的值的第三个位置严格返回True\False。
前五名:
0 True
1 False
2 True
3 True
4 True我不想每排第三个职位。我要每一行的Lower_Minus_1位置。
我试过以下几种方法,但都失败了:
print(df['Password'].str[df['Letter']] == df['Letter'])为每一行返回False,如下所证明:
print((df['Password'].str[df['Letter']] == df['Letter']).sum())返回:0
然后我试了一下:
print(df.apply(lambda x: x['Password'].str[x['Lower_Minus_1']], axis=1) == df['Letter'])这会引发一个错误:
File "D:/AofC/2020_day2.py", line 56, in <lambda>
print(df.apply(lambda x: x['Password'].str[x['Lower_Minus_1']], axis=1) == df['Letter'])
AttributeError: 'str' object has no attribute 'str'发布于 2022-06-02 01:05:48
df.apply(lambda x:x['Letter']== x['Password'][x.Lower_Minus_1], axis=1)
0 True
1 False
2 True
3 True
4 True
dtype: boolhttps://stackoverflow.com/questions/72469487
复制相似问题