python - 迭代所有 Dataframe 行并执行startswith()
问题描述
我的 df 看起来像这样:
如您所见,用户以“ff”开头,它可能位于访问列或任何其他列而不是用户列中。
我想在这个 df 中创建一个名为“UserID”的新列,只要所有列中的 'ff' 都将这个值复制到我的新列“UserId”
我一直在使用这种工作正常的方法,但我必须在所有列中重复这一行:
hist.loc[hist.User.str.startswith("ff",na=False),'UserId']=hist['User'].str[2:]
有没有其他方法可以一次循环遍历所有行?
谢谢
解决方案
If you are cool with picking only the first occurence:
df['UserID'] = df.apply(lambda x: x[x.str.startswith('ff')][:1], axis=1)
推荐阅读
- pyomo - pyomo 中的差异化
- sql - 在树形结构中计算公司中的人员
- c++ - 尝试实例化 bitset 并获取“非类型模板参数不是常量表达式”
- ios - Xcode 9.3 快速帮助(选项+单击)显示为空
- node.js - Hyperledger Fabric 教程“错误:未知标志:--peerAddresses”
- javascript - 基于Javascript的滑块不起作用
- php - 将存储在 JSON 数组中的字符串转换为 PHP 中的变量名
- android - 在 Android Studio 中更改布局后需要清理项目
- sql - SQL如何查找某个位是否打开
- hive - 选择满足 hive 条件的上一行