使用Python/Pandas为特定列值添加/复制行

使用Python/Pandas为特定列值添加/复制行,python,excel,pandas,Python,Excel,Pandas,当特定列具有特定值时,我尝试使用pandas数据框将新的/重复的行插入excel。如果列值为TRUE,则复制该行并更改其值 例如: Input A B C D 0 Red 111 A 2 1 Blue 222 B 12 2 Green 333 B 3 3 Black 111 A 2 4 Yellow 222 D 12 5 Pi

当特定列具有特定值时,我尝试使用pandas数据框将新的/重复的行插入excel。如果列值为TRUE,则复制该行并更改其值

例如:

Input

    A        B      C   D   
0   Red      111    A   2   
1   Blue     222    B   12  
2   Green    333    B   3
3   Black    111    A   2   
4   Yellow   222    D   12  
5   Pink     333    c   3
6   Purple   777    B   10
Output
    A        B      C   D   
0   Red      111    A   2   
1   Blue     222    Y   12  
2   Blue     222    Z   12
3   Green    333    Y   3
4   Green    333    Z   3
5   Black    111    A   2   
6   Yellow   222    D   12  
7   Pink     333    c   3
8   Purple   777    Y   10
9   Purple   666    Z   10
如果您在C列中看到,当遇到specific Value=B时,我只想复制该行。在原始行和复制行中将其值分别更改为Y和Z。(如果我遇到B以外的任何内容,请不要复制。)

使用替换的
C
列,将过滤的行替换为
Z
,将
0.5
添加到索引中,以便始终正确
排序索引

df1 = df.replace({'C': {'B':'Y'}})
df2 = df[df['C'].eq('B')].assign(C = 'Z').rename(lambda x: x + .5)

df = pd.concat([df1, df2]).sort_index().reset_index(drop=True)
print (df)
        A    B  C   D
0     Red  111  A   2
1    Blue  222  Y  12
2    Blue  222  Z  12
3   Green  333  Y   3
4   Green  333  Z   3
5   Black  111  A   2
6  Yellow  222  D  12
7    Pink  333  c   3
8  Purple  777  Y  10
9  Purple  777  Z  10
或者创建3个小数据帧,不带
B
值,过滤并设置值和
concat

mask = df['C'].eq('B')
df0 = df[~mask]
df1 = df[mask].assign(C = 'Y')
df2 = df[mask].assign(C = 'Z').rename(lambda x: x + .5)

df = pd.concat([df0, df1, df2]).sort_index().reset_index(drop=True)
替代办法

#Replace B with Y & Z first in column C
df.replace({'C': {'B': 'Y,Z'}}, inplace = True)

#Use "explode" Avaible on pandas 0.25 to split the value into 2 columns
df=df.assign(C=df.C.str.split(",")).explode('C')