pandas - 根据另一列更改另一列的值
问题描述
我有一个这样的数据框:
Index Latitude Longitude Wave Height Wave Period
0 7.101 101 0.3 4.1
1 7.103 101 0.25 4.2
2 7.105 101 0.5 4.4
3 0 0 0.6 4.6
4 0 0 0.7 4.8
5 7.1 101 0.3 4.1
6 7.1 101 0.3 4.3
7 7.1 101 0.3 4.4
8 0 0 0.6 4.6
9 0 0 0.7 4.8
10 7.1 101 0.3 4.1
如果纬度和经度为零,我想将波高和波周期值更改为零。
期望的输出:
Index Latitude Longitude Wave Height Wave Period
0 7.101 101 0.3 4.1
1 7.103 101 0.25 4.2
2 7.105 101 0.5 4.4
3 0 0 0 0
4 0 0 0 0
5 7.1 101 0.3 4.1
6 7.1 101 0.3 4.3
7 7.1 101 0.3 4.4
8 0 0 0 0
9 0 0 0 0
10 7.1 101 0.3 4.1
解决方案
你可以使用pd.loc
:
df.loc[df['Latitude'].eq(0) & df['Longitude'].eq(0),
['Wave Height', 'Wave Period']] = 0
输出:
Index Latitude Longitude Wave Height Wave Period
0 7.101 101 0.30 4.1
1 7.103 101 0.25 4.2
2 7.105 101 0.50 4.4
3 0 0 0.00 0.0
4 0 0 0.00 0.0
5 7.100 101 0.30 4.1
6 7.100 101 0.30 4.3
7 7.100 101 0.30 4.4
8 0 0 0.00 0.0
9 0 0 0.00 0.0
10 7.100 101 0.30 4.1
推荐阅读
- json - 如何在 Angular 6 中过滤复杂的结构化 Json 数据
- c# - 我的代码可能有什么问题?
- azure - 在不使用 ARM 模板的情况下将证书部署到服务结构集群
- shell - sendmail 在 crontab 中不起作用
- angular - angular4-data-table 修复标题
- matlab - 迭代模拟的严重性能问题
- ssl - kafka 中的 SSL 错误:入站在接收对等方的 close_notify 之前关闭
- ios - iOS - NSTimeZone 的 3 字母时区缩写
- c++ - C++:当我使用文件描述符从日志文件中读取时,识别日志文件何时被移动/重命名/删除
- ios - CAGradientLayer:其他 UIObjects 没有出现