简体   繁体   English

如何在不迭代每一列的情况下有条件地将 dataframe 的一列中的值替换为另一列的值?

[英]How to conditionally replace the value from one column of a dataframe with the value of another without iterating each column?

given columns street, city, state, zip and columns new_street, new_city, new_state, new_zip.给定列街道、城市、state、zip 和列 new_street、new_city、new_state、new_zip。

where the 'new' columns sometimes have updated data but are sometimes null.其中“新”列有时会更新数据,但有时是 null。

I basically want to say我基本上想说

if df['new_street'] != null:
    df['street'] = df['new_street']
    df['city'] = df['new_city']
#etc

but i want it to check each row for the condition, not the series.但我希望它检查每一行的条件,而不是系列。 the way I am currently solving this problem is using apply我目前解决这个问题的方式是使用 apply

#this code block is super inefficient, there is probably
#a vectorized way to achieve this but i dont know it


def updated_street(row):
    if row['_merge'] == 'both':
        return row['Current Delivery Address']
    return row['street']
        
def updated_city(row):
    if row['_merge'] == 'both':
        return row['Current City']
    return row['city']
    
def updated_state(row):
    if row['_merge'] == 'both':
        return row['Current State']
    return row['state']
    
def updated_zip(row):
    if row['_merge'] == 'both':
        return row['Current ZIP+4']
    return row['zip']

#if we merged in new address info, replace the old with the new
df['street'] = df.apply(updated_street, axis=1)
df['city'] = df.apply(updated_city, axis=1)
df['state'] = df.apply(updated_state, axis=1)
df['zip'] = df.apply(updated_zip, axis=1)

but of course that rewrites every cell in each column.但当然,这会重写每列中的每个单元格。

I would loop through the columns and use np.where and have a formatted string beginning with new_ to deal with those dynamic new_ columns.我将遍历列并使用np.where并使用以new_开头的格式化字符串来处理那些动态new_列。 This solution is vectorized with np.where() with a total length of the loop being 2 for the two columns.该解决方案使用np.where()进行矢量化,两列的循环总长度为2

import numpy as np
for col in ['street', 'city']:
    df[col] = np.where(df['new_street'] != np.nan, df[f'new_{col}'], df[col])

output: No output as I couldn't test. output:没有 output,因为我无法测试。 Please provide input data:)请提供输入数据:)

暂无
暂无

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

相关问题 如何通过迭代替换 dataframe 列的某个值 - how to replace certain value of a dataframe column by iterating Python:如何在不删除重复项的情况下从另一个 dataframe 替换列值 - Python: how to replace a column value in one dataframe from another without removing duplicates 如何用另一列中的值替换 DataFrame 列中值的所有实例 - How to replace all instances of a value in one DataFrame column with a value from another column 如何有条件地将 dataframe 中的值从一列复制到另一列? - how can I copy value from one column to another in dataframe conditionally? 如何用另一个值替换数据框列中的空白? - How to replace the blanks in a column of a dataframe with another value? 如何迭代每一行并从一个 dataframe 的特定列中找到下一个匹配列值并将其与另一个 dataframe 进行比较? - How to iterate each row and find the next matching column value from a specific column from one dataframe and comparing it to another dataframe? 用熊猫中另一列的每个值替换一个列的值 - Replace values of one column for each value of another column in pandas 如何根据同一 dataframe 中另一列的值替换 Dataframe 中列中的 NaN 值 - How to replace NaN value in column in Dataframe based on values from another column in same dataframe 替换数据框列中的值,直到达到特定的有条件变化的值 - Replace value in dataframe column until specific conditionally changing value is reached 将数据框子节中的1值替换为另一列的值 - Replace 1 value in a subsection of dataframe with value of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM