简体   繁体   English

用另一列中的相同行值替换 pandas dataframe 列中的值

[英]Replacing values in pandas dataframe column with same row value from another column

I have a pandas dataframe that looks like this:我有一个 pandas dataframe 看起来像这样:

            val_1   val_2   Flag
Date                       
2018-08-27  221.0  121.0     0
2018-08-28  222.0  122.0     1
2018-08-29  223.0  123.0     0
2018-08-30  224.0  124.0     2
2018-08-31  225.0  125.0     0

I want to change the Flag column values to the same values from other columns based on Flag condition.我想根据标志条件将标志列值更改为其他列的相同值。 Namely, if Flag is 1 replace 1 with val_1 from the same row and if Flag is 2 replace it with val_2.即,如果 Flag 为 1,则将同一行中的 1 替换为 val_1,如果 Flag 为 2,则将其替换为 val_2。 The output that I am looking would look like this:我正在寻找的 output 看起来像这样:

            val_1   val_2   Flag
Date                       
2018-08-27  221.0  121.0     0
2018-08-28  222.0  122.0     222.0
2018-08-29  223.0  123.0     0
2018-08-30  224.0  124.0     124.0
2018-08-31  225.0  125.0     0

I know that I can use .loc like this df.loc[df['Flag'] == 1, ['Flag']] = .我知道我可以像这样使用.loc df.loc[df['Flag'] == 1, ['Flag']] = I don't know what goes to the right hand side of the code.我不知道代码右侧是什么。

IIUC:国际大学联盟:

new_vals = df.lookup(df.index, df.columns[df.Flag-1])

df['Flag'] = df.Flag.mask(df.Flag>0, new_val)

Note : as commented by @Erfan, this would also work:注意:正如@Erfan 所评论的,这也可以:

df['Flag'] = df.lookup(df.index, df.columns[df.Flag-1])

Output: Output:

            val_1  val_2  Flag
Date                          
2018-08-27  221.0  121.0     0
2018-08-28  222.0  122.0   222
2018-08-29  223.0  123.0     0
2018-08-30  224.0  124.0   124
2018-08-31  225.0  125.0     0

One other way is to use np.where for numpy.where(condtion,yes,no)另一种方法是将 np.where 用于numpy.where(condtion,yes,no)

In this case, I use nested np.where so that在这种情况下,我使用嵌套np.where以便

np.where(If Flag=2,take val_2,(take x)) where takex is another np.where

df['Flag']=np.where(df['Flag']==1,df['val_1'],(np.where(df['Flag']==2,df['val_2'],df['Flag'])))
df

Output Output

在此处输入图像描述

Few ways you could do this, firstly your initial code is very close, you just need to end the assignment:有几种方法可以做到这一点,首先你的初始代码非常接近,你只需要结束分配:

df.loc[df['Flag'] == 1, 'Flag'] = df['val_1']
print(df)
         Date  val_1  val_2   Flag
0  2018-08-27  221.0  121.0    0.0
1  2018-08-28  222.0  122.0  222.0
2  2018-08-29  223.0  123.0    0.0
3  2018-08-30  224.0  124.0    2.0
4  2018-08-31  225.0  125.0    0.0

what you're doing here is filtering your dataframe and replacing the values where the conditions matches.你在这里做的是过滤你的 dataframe 并替换条件匹配的值。 in this iinstance where Flag is equal to one.在这种情况下,Flag 等于 1。

since you're making muliple assingments, lets use np.select既然你正在做多重评估,让我们使用np.select

import numpy as np
conditions = [df['Flag'].eq(1),
             df['Flag'].eq(2)]


choices = [df['val_1'],df['val_2']]

df['Flag'] = np.select(conditions,choices,default=df['Flag'])

What this this does is evaulate any and all conditions you have.这样做的目的是评估您拥有的所有条件。 leaving the default as the original column.将默认值保留为原始列。 You can add more conditions in, and wrap OR statements in parenthsis with a |您可以在其中添加更多条件,并将 OR 语句用 | 括在括号中。 (pipe) sepreators. (管道)分离器。 ie [(df['Flag'] == 1 | df['Flag'] == 2)][(df['Flag'] == 1 | df['Flag'] == 2)]

         Date  val_1  val_2   Flag
0  2018-08-27  221.0  121.0    0.0
1  2018-08-28  222.0  122.0  222.0
2  2018-08-29  223.0  123.0    0.0
3  2018-08-30  224.0  124.0  124.0
4  2018-08-31  225.0  125.0    0.0

暂无
暂无

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

相关问题 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe 用 pandas dataframe 中同一列上的不同值替换缺失值 - replacing the missing value with different values on the same column in pandas dataframe 用另一个值替换熊猫数据框列中的几个值 - Replacing few values in a pandas dataframe column with another value 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 用另一列中的值替换pandas列中的值 - Replacing pandas column value with values from another column Pandas:如何使用其他 dataframe 的列值从 dataframe 返回具有相同行值的行? - Pandas: How to return the row from dataframe having same row values by using column value of other dataframe? 当同一行中的另一列为NaN时,如何从熊猫数据框中选择特定的列值? - How to select a particular column value from a pandas dataframe when another column in the same row is NaN? 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值 - Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame 替换 Pandas DataFrame 中的列值 - Replacing column values in a pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM