简体   繁体   English

如何根据 pandas 中两个不同列的条件将值从一列复制到另一列?

[英]how to copy values from one column into another column based on conditions of two different columns in pandas?

Hi I am trying to copy values from one column to another column based on conditions.您好我正在尝试根据条件将值从一列复制到另一列。 But no luck for it.但没有运气。

Code:代码:


import pandas as pd
df1 = pd.read_csv('file.txt',sep='\t')
id_array = list(df1['ID_1'].unique())

df1['id2_in_id1?'] = df1['ID_2'].apply(lambda a : 'Yes' if a in id_array else 'No')#was trying to create condition for copy

Input:输入:

ID_1    ID_2    branchID         fill?
ABC1               1              yes
DAC1    ABC1                      no
TAC1    ABC1                      no
MAK1    ABC1                      no
TBG1    ABC1                      no
DEF1               2              no
VAX1    DEF1                      no

Expected Output:预期 Output:

ID_1    ID_2    branchID    smallbrancid
ABC1                1            
DAC1    ABC1                     1
TAC1    ABC1                     1
MAK1    ABC1                     1
TBG1    ABC1                     1
DEF1                2             
VAX1    DEF1                     


DO with two mask + ffill做两个mask + ffill

df['smallbrancid']=df.branchID.mask(df.branchID.eq('')).ffill().mask(df.branchID.ne(''),'')
df
Out[37]: 
   ID_1  ID_2 branchID smallbrancid
0  ABC1     -        1            -
1  DAC1  ABC1        -            1
2  TAC1  ABC1        -            1
3  MAK1  ABC1        -            1
4  TBG1  ABC1        -            1
5  DEF1     -        2            -
6  VAX1  DEF1        -            2

暂无
暂无

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

相关问题 如何根据 pandas 中多列的条件替换列中的值 - How to replace values in a column based on conditions from multiple columns in pandas 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas 如何根据 Pandas 中的条件将一列的值复制到另一列? - How to copy values of one column to another based on condition in Pandas? 如何根据不同列中的值向 pandas dataframe 添加一列? - How to add one column to pandas dataframe based on values in different columns? 如何根据条件将 Pandas 中的列从一个数据集复制到另一个数据集 - How to copy a column in Pandas from one dataset to another based on condition 如何基于另一列的值(其中两个不同的列中的值在pandas中相等)找到一列的行索引? - How to find row index of one column based on values of a different column where the values in the two distinct columns are equal in pandas? 根据条件将值从一列到另一列熊猫数据框中 - Give values from one column to another column in pandas dataframe based on conditions 如何将两个不同列中的唯一值复制到第三列 - how to copy unique values from two different columns into third column Pandas:根据两个不同的列条件选择值价格 - Pandas: select the values price based on two different column conditions 如何根据 pandas 中的另一列中的值将列中的值放入一行中的新列中? - How to put values in a column into new columns in one row based on values in one another column in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM