首页 > 解决方案 > 根据另一列更改另一列的值

问题描述

我有一个这样的数据框:

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

标签: pandasdataframe

解决方案


你可以使用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

推荐阅读