带有 "wildcard" 的条件语句
Conditional Statement with a "wildcard"
在 table A 中,有第 1 列和第 2 列。
- 第 1 列是唯一 ID,例如 ('A12324'),第 2 列暂时为空。
- 如果 id 以 A 开头,我想用 Yes 填充第 2 列的值,否则用 No。
有人知道我可以如何使用左手吗?
我试过了,但我的错误是 left is not defined。
TableA.loc[TableA['col1'] == left('A',1), 'hasAnA'] = 'Yes'
您可以使用pd.Series.str.startswith()
方法:
>>> frame = pd.DataFrame({'colA': ['A12342', 'B123123231'], 'colB': False})
>>> condition = frame['colA'].str.startswith('A')
>>> frame.loc[condition, 'colB'] = 'Yes'
>>> frame
colA colB
0 A12342 Yes
1 B123123231 False
在 table A 中,有第 1 列和第 2 列。
- 第 1 列是唯一 ID,例如 ('A12324'),第 2 列暂时为空。
- 如果 id 以 A 开头,我想用 Yes 填充第 2 列的值,否则用 No。
有人知道我可以如何使用左手吗?
我试过了,但我的错误是 left is not defined。
TableA.loc[TableA['col1'] == left('A',1), 'hasAnA'] = 'Yes'
您可以使用pd.Series.str.startswith()
方法:
>>> frame = pd.DataFrame({'colA': ['A12342', 'B123123231'], 'colB': False})
>>> condition = frame['colA'].str.startswith('A')
>>> frame.loc[condition, 'colB'] = 'Yes'
>>> frame
colA colB
0 A12342 Yes
1 B123123231 False