How to do a substring using pandas or numpy
<p>I'm trying to do a substring on data from column "ORG".  I only need the 2nd and 3rd character.  So for 413 I only need 13.  I've tried the following:</p>

<pre><code>Attempt 1:  dr2['unit'] = dr2[['ORG']][1:2]
Attempt 2:  dr2['unit'] = dr2[['ORG'].str[1:2]
Attempt 3:  dr2['unit'] = dr2[['ORG'].str([1:2])
</code></pre>

<p>My dataframe:</p>

<pre><code>    REGION  ORG
90       4  413
91       4  413
92       4  413
93       5  503
94       5  503
95       5  503
96       5  503
97       5  504
98       5  504
99       1  117
100      1  117
101      1  117
102      1  117
103      1  117
104      1  117
105      1  117
106      3    3
107      3    3
108      3    3
109      3    3
</code></pre>

<p>Expected output:</p>

<pre><code>    REGION  ORG  UNIT
90       4  413  13
91       4  413  13
92       4  413  13
93       5  503  03
94       5  503  03
95       5  503  03
96       5  503  03
97       5  504  04
98       5  504  04
99       1  117  17
100      1  117  17
101      1  117  17
102      1  117  17
103      1  117  17
104      1  117  17
105      1  117  17
106      3    3  03
107      3    3  03
108      3    3  03
109      3    3  03
</code></pre>

<p>thanks for any and all help!</p>

<p>Your square braces are not matching and you can easily slice with <code>[-2:]</code>.</p>

<p><em>apply</em> <code>str.zfill</code> with a width of 2 to pad the items in the new series:</p>

<pre><code>&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; ld = [{'REGION': '4', 'ORG': '413'}, {'REGION': '4', 'ORG': '414'}]
&gt;&gt;&gt; df = pd.DataFrame(ld)
&gt;&gt;&gt; df
   ORG REGION
0  413      4
1  414      4
&gt;&gt;&gt; df['UNIT'] = df['ORG'].str[-2:].apply(str.zfill, args=(2,))
&gt;&gt;&gt; df
   ORG REGION UNIT
0  413      4   13
1  414      4   14
2    3      4   03
</code></pre>

python pandas numpy