简体   繁体   English

如何使用赋值运算符在另一个条件下在DataFrame的多个列中放置更新值?

[英]How to in-place update values in multiple columns in a DataFrame on condition from another using assignment operator?

There is one DataFrame S to be updated: 有一个要更新的DataFrame S

n ii      a  b     c
0 True   10 11  1.20 
1 False   2  0   NaN
2 True   34 75  2.14
3 True   22 88  0.02

from another DataFrame T with another set of columns 从另一个带有另一组列的DataFrame T

 a  b     c
 8 13  1.19
31 72  2.10
20 83  0.05

Is it possible to have S updated in a function with one line assignment statement? 是否可以使用一个行分配语句在一个函数中更新S

def process(S):
    ii = S.ii
    # ... internal calculations that produce T
    columns = ['a', 'b', 'c']
    S[ii][columns] = T[columns] # < ----- in-place update

That process works on a pass-by-reference approach leaving S updated after a call process适用于按引用传递方法,使呼叫后S更新

process(S)

Try returning the dataframe: 尝试返回数据框:

    def process(S):
        ii = S.ii
        # ... internal calculations that produce T
        columns = ['a', 'b', 'c']
        S.loc[ii, columns] = T[columns] # < ----- in-place update
        return S

And call the function like this: 并像这样调用函数:

    S = process(S)

暂无
暂无

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

相关问题 Pandas Dataframe中的值的就地更新 - In-Place Update of Values in Pandas Dataframe 根据多列的条件从另一个 dataframe 更新列的某些值 - Update certain values of a column from another dataframe based on condition of multiple columns 如何根据使用 Pyspark 的条件从另一个表更新 Spark DataFrame 表的列值 - How to update Spark DataFrame Column Values of a table from another table based on a condition using Pyspark 如何基于另一个数据框的特定列的值更新数据框的多个列中的值 - How to update values in multiple columns of a dataframe based on value of specific columns of another dataframe 如何使用熊猫中另一个数据框的值更新一个数据框 - How to update one dataframe using values from another dataframe in pandas 在不使用额外内存的情况下从数组中删除值 - Remove values from an array in-place without using extra memory 当索引和列不匹配时,如何使用另一个 dataframe 的值更新 dataframe - How to update a dataframe with values from another dataframe when indexes and columns don't not match 根据来自另一个 DataFrame 的值更新 pandas 列中的值 - Update values in pandas columns based on values from another DataFrame 子集根据另一个数据帧的值在多个列上进行pandas数据帧 - Subset pandas dataframe on multiple columns based on values from another dataframe Dataframe 链接操作以使用具有特定条件的另一列中的值更新列 - Dataframe chaining operations to update a column using values from another column with specific condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM