python - Pandas 使用 If 语句逐行执行
问题描述
如果其他人对标题有更好的想法,我不确定这是否是最好的标题,我很乐意提出建议。
假设我有一个如下所示的数据框:
df2
A section
0 <fruit>
1 apple
2 orange
3 pear
4 watermelon
5 </fruit>
6 <furniture>
7 chair
8 sofa
9 table
10 desk
11 </furniture>
我想要的是一个看起来像这样的数据框:
A section
0 <fruit> fruit
1 apple fruit
2 orange fruit
3 pear fruit
4 watermelon fruit
5 </fruit> fruit
6 <furniture> furniture
7 chair furniture
8 sofa furniture
9 table furniture
10 desk furniture
11 </furniture> furniture
有没有办法做到这一点?我考虑过使用 if 语句逐行执行,但是在执行此操作时遇到了布尔逻辑问题。
编辑#1:
下面发布的这个解决方案解决了我的问题。
解决方案:
df['section']=pd.Series(np.where(df.A.str.contains('<'),df.A.str.replace('<|>|/',''),np.nan)).ffill()
如果我有这样的数据怎么办?我想要同样的结果。
A section
0 <fruit>
1 <fruit_1>apple</fruit_1>
2 <fruit_2>orange</fruit_2>
3 <fruit_3>pear</fruit_3>
4 <fruit_4>watermelon</fruit_4>
5 </fruit>
6 <furniture>
7 <furniture_1>chair</furniture_1>
8 <furniture_2>sofa</furniture_2>
9 <furniture_3>table</furniture_3>
10 <furniture_4>desk</furniture_4>
11 </furniture>
解决方案
IIUC 使用contains
查找行,并np.where
分配值,然后使用ffill
填充np.nan
df['section']=pd.Series(np.where(df.A.str.contains('<'),df.A.str.replace('<|>|/',''),np.nan)).ffill()
df
Out[1003]:
A section
0 <fruit> fruit
1 apple fruit
2 orange fruit
3 pear fruit
4 watermelon fruit
5 </fruit> fruit
6 <furniture> furniture
7 chair furniture
8 sofa furniture
9 table furniture
10 desk furniture
11 </furniture> furniture
如果您想更精确/具体/更严格,您还可以使用 and 检查字符串的开始和startswith
结束endswith
。
df1['Section'] = pd.Series(np.where(df1.A.str.startswith('<') & df1.A.str.endswith('>'), df1.A.str.replace('<|>|/',''), np.nan)).ffill()
推荐阅读
- python - Microsoft MSAL 是否具有资源所有者密码凭据授予授权支持?
- python - 用它们的乘法列表替换数字列表
- javascript - 在 React 中使用 canvas html5 的清晰方法,无需始终渲染所有画布形状
- node.js - 在 reactjs 中过滤表数据
- json - 在 dart 中显示 itams 的问题
- php - 每个字段的唯一序列与 php 和 mysql
- mule - 在 mule 4 中进行批处理后获取输出
- javascript - 如何在 AWS Lambda 函数中连接 node-imap 模块
- python - 如何继续添加更多 json 信息
- apache-spark - 从 Spark 中的 executor/worker 中检索本地对象