pandas python inserting part of a column to a column based on conditions pandas python <p>i have a large dataset that i want to work with , but here i am using a mock dataset:</p> <pre><code>data = {'Block': [1, 1, 1, 1, 1, 1,1,1,1], 'Concentration': [100, 100, 100, 33, 33, 33, 0,0,0], 'Name' : ['A', 'A', 'A', 'A', 'A', 'A', 'PB', 'PB', 'PB'], 'value': [86, 194, 452, 140, 285, 2011, 100, 111, 222 ]} data = DataFrame(data) </code></pre> <p>looks like this:</p> <pre><code>In [12]: data Out[12]: Block Concentration Name value 0 1 100 A 86 1 1 100 A 194 2 1 100 A 452 3 1 33 A 140 4 1 33 A 285 5 1 33 A 2011 6 1 0 PB 100 7 1 0 PB 111 8 1 0 PB 222 </code></pre> <p><strong>there are a total of 24 Blocks, 3 types of concentrations and 5 Names for each Block.</strong> </p> <p>i want for each block to add 3 new '0' concentrations for each Name other than the name 'PB', and then append the values from 'PB' to the newly added '0' concentrations. </p> <p>for the mock dataset here the desired output would be:</p> <pre><code>In [13]: data2 Out[13]: Block Concentration Name value 0 1 100 A 86 1 1 100 A 194 2 1 100 A 452 3 1 33 A 140 4 1 33 A 285 5 1 33 A 2011 6 1 0 A 100 7 1 0 A 111 8 1 0 A 222 9 1 0 PB 100 10 1 0 PB 111 11 1 0 PB 222 </code></pre> <p>my code so far , im able to grab out only the 'PB' rows for each block:</p> <pre><code>def PBvalue(sgrp): PBvalue = sgrp.loc[data['Name']=='PB'].copy() return PBvalue PBvalues = data.groupby(['Block', 'Concentration']).apply(PBvalue) </code></pre> <p>output:</p> <pre><code>In [30]: PBvalues Out[30]: Block Concentration Name value Block Concentration 1 0 6 1 0 PB 100 7 1 0 PB 111 8 1 0 PB 222 </code></pre>