abu*_*nte 5 python string dataframe pandas
我有这个数据框:
Code Mark
0 Abd 43212312312
1 Charles de Gaulle
2 Carlitos 4132411
3 Antonio
Run Code Online (Sandbox Code Playgroud)
如果代码列中字符串的最后 5 个字符是数字,我希望 'Mark' 是 'A',所以它看起来像这样:
Code Mark
0 Abd 43212312312 A
1 Charles de Gaulle
2 Carlitos 4132411 A
3 Antonio
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用isnumeric,但我不断收到 AttributeError:'Series' object has no attribute 'isnumeric'
有人可以帮忙吗?
你很近。诀窍是通过使用.str访问器pd.Series.str.isnumeric。
然后通过以下方式映射到 'A' 或空字符串pd.Series.map:
df['Mark'] = df['Code'].str[-5:]\
.str.isnumeric()\
.map({True: 'A', False: ''})
print(df)
Code Mark
0 Abd43212312312 A
1 CharlesdeGaulle
2 Carlitos4132411 A
3 Antonio
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3375 次 |
| 最近记录: |