python - Pandas Max or Min Row value in a new column based on value another colum
问题描述
So there are 3 columns in the pandas dataframe
I want to create 4th column D, which would based on a condition that if A > 5 then D would have a value of B else value of C. This is for each row
A,B,C
1,1,2
6,2,3
7,3,1
For example, if you see for the new column D
D row1 = 1 # because A < 5, row of B would be chosen
D row2 = 3 # because A > 5, row of C would be chosen
D row3 = 1 # because A > 5, row of C would be chosen
So the final dataframe would be like
A,B,C,D
1,1,2,1
6,2,3,3
7,3,1,1
解决方案
I think you can use
import numpy as np
df['D'] = np.where(df['A']>5, df['C'], df['B'])
推荐阅读
- java - 任务 ':rtmp_publisher:compileReleaseKotlin' 执行失败
- ios - 使用 GIF 图像滚动时 UICollectionView 滞后
- ruby-on-rails - 在 ubuntu 上安装画布时出现问题,Bundler 找不到 gem "bundle" 的兼容版本
- asp.net-core - 上证所事件源 Asp.net Core 3.1
- javascript - 使用 SMTP.js 时如何隐藏我的 gmail.smtp 密码?
- python - 使用python从网站下载视频
- computer-vision - 如何在 Blender 中从不同的摄像机角度渲染 3d 模型的不同贴图(漫反射、深度、镜面反射、阴影)?
- gitlab - 在 Gitlab 中未显示 Plantuml 图
- java - 如何将多个字符串变量放置在 3d 数组的一个坐标中?
- c++ - 如何在控制台应用程序中使用 C++ WinRT 注册事件?