简体   繁体   English

Pandas 根据同一数据框中另一列的条件替换列值

[英]Pandas Replace column values based on condition upon another column in the same data frame

I have a data frame as below:我有一个数据框如下:

在此处输入图像描述

I want to copy the selected values of easting and northing to the column longitude and latitude if northing is < 90 and then set the original values to None.如果北距< 90,我想将选择的东距和北距值复制到经度和纬度列,然后将原始值设置为无。

Here is my python code:这是我的 python 代码:

for i, j in waypoint_df.iterrows():
    easting = j['easting']
    northing = j['northing']
    if northing and northing < 90:
        waypoint_df.at[i,'latitude'] = northing
        waypoint_df.at[i,'longitude'] = easting
        waypoint_df.at[i,'northing'] = None
        waypoint_df.at[i,'easting']= None

Is there a simpler way to run the operation above instead of iterating all rows?有没有更简单的方法来运行上面的操作而不是迭代所有行?

Use pandas.DataFrame.assign to swap the values of eas/nor and lon/lat with a boolean mask.使用pandas.DataFrame.assign将 eas/nor 和 lon/lat 的值与 boolean 掩码交换。

mask = waypoint_df['northing'].lt(90)

waypoint_df.loc[mask] = waypoint_df.assign(latitude= waypoint_df['easting'], easting= waypoint_df'[latitude'],
                         longitude= waypoint_df['northing'], northing= waypoint_df['longitude'])
# Output: # Output:
print(waypoint_df)

   locality_id locality   waypoint   easting   northing                                     description   grid      date collected_by  latitude  longitude  waypoint_pk
0          761     None  BATurnoff  368255.0  6614695.0   Turnoff from Yarri Rd (also access to Harpers  AMG51  12-12-07          SJB       NaN        NaN            1
1          761     None       BKAW  367700.0  6616800.0                End of access track at breakaway  AMG51  12-12-07          SJB       NaN        NaN            2
2          761     None      BKAWT  367071.0  6615492.0   Access track turnoff from Harpers Lagoon road  AMG51  12-12-07          SJB       NaN        NaN            3
3         3581     None    DM14-01       NaN        NaN   King of the Hills dyke -- Hugely contaminated   None  10-11-15       D Mole     121.2     -28.62            4
4         3581     None    DM14-02       NaN        NaN  Intrusion into KOTH dyke? -- Graphic granite -   None  10-11-15       D Mole     121.2     -28.62            5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 pandas 根据同一数据帧中另一列的条件获取列值的连续平均值 - How to get consecutive averages of the column values based on the condition from another column in the same data frame using pandas 大熊猫:根据索引和列将一个数据框的值替换为另一数据框的值 - Pandas: replace values of one data frame with values of another data frame based on index and column 根据 pandas 数据框中另一列中的条件对一列求和 - Summing a column based on a condition in another column in a pandas data frame 在 Pandas 数据框中快速搜索并根据条件在数据框的另一列中插入值 - Fast search in pandas data frame and inserting values in another column of the data frame based on a condition 根据条件替换熊猫数据框列中的值 - Replace value in a pandas data frame column based on a condition 根据 Pandas 中的条件删除列中的值 - Deleting values in the column based upon condition in Pandas 从具有基于另一列的条件的 pandas 数据帧中删除重复项 - Removing duplicates from pandas data frame with condition based on another column 根据if条件为熊猫数据框中的列分配值 - assigning values to column in pandas data-frame based on if condition 根据为pandas中另一个数据框中的列提供的条件对数据框的列执行操作 - perform operation on column of data frame based on condition given to column in another data frame in pandas Python Pandas 数据帧 基于另一列的计数值 - Python Pandas Data Frame Count values of one column based on another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM