简体   繁体   English

按条件重复数据框中的单元格值

[英]Repeat the cell value in dataframe by condition

As the dataframe d1 below, A1 is correspond to B1 , A2 is correspond to B2 , and so on. 如下面的数据帧d1所示, A1对应于B1A2对应于B2 ,依此类推。 I want to change B1-3 value by condition of: B or C = copy 2 times, D = copy 3 times, as the dataframe target . 我想通过以下条件更改B1-3值: BC =复制2次​​, D =复制3次,作为数据帧目标

d1 = DataFrame([{'A1': 'A', 'A2': 'A', 'A3': '', 'B1': '2', 'B2': '2', 'B3': ''},
                {'A1': 'A', 'A2': 'C', 'A3': '', 'B1': '2', 'B2': '2', 'B3': ''},
                {'A1': 'A', 'A2': 'B', 'A3': 'C', 'B1': '2', 'B2': '4', 'B3': '4'},
                {'A1': 'A', 'A2': 'C', 'A3': 'D', 'B1': '2', 'B2': '2', 'B3': '4'}])

d1
    A1  A2  A3  B1  B2  B3
0   A   A       2   2   
1   A   C       2   2   
2   A   B   C   2   4   4
3   A   C   D   2   2   4
target = DataFrame([{'A1': 'A', 'A2': 'A', 'A3': '', 'B1': '2', 'B2': '2', 'B3': ''},
                {'A1': 'A', 'A2': 'C', 'A3': '', 'B1': '2', 'B2': '22', 'B3': ''},
                {'A1': 'A', 'A2': 'B', 'A3': 'C', 'B1': '2', 'B2': '44', 'B3': '44'},
                {'A1': 'A', 'A2': 'C', 'A3': 'D', 'B1': '2', 'B2': '22', 'B3': '444'}])
target

    A1  A2  A3  B1  B2  B3
0   A   A       2   2   
1   A   C       2   22  
2   A   B   C   2   44  44
3   A   C   D   2   22  444 

And I've tried using np.where for the condition of B and C , but it's seems only apply on B to copy the value. 而且我尝试使用np.where来处理BC的条件,但似乎仅适用于B复制值。 Is there any methods to reach it. 有什么方法可以达到目的。

Acol = ['A1','A2','A3']
Bcol = ['B1','B2','B3']
d1[Bcol] = np.where(d1[Acol] == ('B' or 'C'), d1[Bcol]+d1[Bcol], d1[Bcol])
d1

    A1  A2  A3  B1  B2  B3
0   A   A       2   2   
1   A   C       2   2   
2   A   B   C   2   44  4
3   A   C   D   2   2   4

I'd suggest storing the multiplier conditions for A, B, … in a dictionary and applying it like this: 我建议将A,B,…的乘法器条件存储在字典中,然后像这样应用它:

multiplier_map={'':1,'A':1,'B':2,'C':2,'D':3}
for i in [1,2,3]:
    df['B{0}'.format(i)]=df['B{0}'.format(i)]*df['A{0}'.format(i)].map(multiplier_map)

Note that the multiplier_map also needs to contain an empty string as key. 请注意, multiplier_map还需要包含一个空字符串作为键。

Using np.select 使用np.select

for col in ('A1','A2','A3'):
    new_col = 'B'+col[-1]
    mask1 = df[col] == 'A'
    mask2 = (df[col] == 'B') | (df[col] == 'C')
    mask3 = df[col] == 'D'
    df[new_col] = df[new_col].astype('str')
    df[new_col] = np.select([mask1, mask2, mask3], [df[new_col], df[new_col]*2, df[new_col]*3], df[new_col])

Output: 输出:

    A1  A2  A3  B1  B2  B3
0   A   A       2   2   
1   A   C       2   22  
2   A   B   C   2   44  44
3   A   C   D   2   22  444

Maybe these four lines: 也许这四行:

d1.loc[d1['A2'].eq('B') | d1['A2'].eq('C'), 'B2'] += d1.loc[d1['A2'].eq('B') | d1['A2'].eq('C'), 'B2']
d1.loc[d1['A2'].eq('D'), 'B2'] += d1.loc[d1['A2'].eq('D'), 'B2'] + d1.loc[d1['A2'].eq('D'), 'B2']
d1.loc[d1['A3'].eq('B') | d1['A3'].eq('C'), 'B3'] += d1.loc[d1['A3'].eq('B') | d1['A3'].eq('C'), 'B3']
d1.loc[d1['A3'].eq('D'), 'B3'] += d1.loc[d1['A3'].eq('D'), 'B3'] + d1.loc[d1['A3'].eq('D'), 'B3']

And now: 现在:

print(df)

Is: 方法是:

  A1 A2 A3 B1  B2   B3
0  A  A     2   2     
1  A  C     2  22     
2  A  B  C  2  44   44
3  A  C  D  2  22  444

Try below: 请尝试以下方法:

d1['B1'] = np.where( d1['A1'].isin(['B' , 'C']), d1['B1'] * 2, np.where(d1['A1'].isin(['D']), d1['B1'] * 3, d1['B1']))
d1['B2'] = np.where( d1['A2'].isin(['B' , 'C']), d1['B2'] * 2, np.where(d1['A2'].isin(['D']), d1['B2'] * 3, d1['B2']))
d1['B3'] = np.where( d1['A2'].isin(['B' , 'C']), d1['B3'] * 2, np.where(d1['A3'].isin(['D']), d1['B3'] * 3, d1['B3']))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM