<p>我在pandas中有一个这样格式的数据帧</p>
<pre><code>(df)
School ID Column 1
School 1 AD6000
School 2 3000TO4000
School 3 5000TO6000
School 4 AC2000
School 5 BB3300
School 6 9000TO9900
....
</code></pre>
<p>我要做的就是将第1列中包含单词“to”作为分隔符的行拆分为两个新列,同时保留原始列。结果就是这样</p>
<pre><code>(df)
School ID Column 1 Column 2 Column 3
School 1 AD6000 NaN NaN
School 2 3000TO4000 3000 4000
School 3 5000TO6000 5000 6000
School 4 AC2000 NaN NaN
School 5 BB3300 NaN NaN
School 6 9000TO9900 9000 9900
....
</code></pre>
<p>这是我认为有效的代码,但结果是在第2列和第3列中留下空格,而不是将to左右的数字拆分为各自的列</p>
<pre><code>df[['Column 2','Column 3']] = df['Column 1'].str.extract(r'(\d+)TO(\d+)')
</code></pre>
<p>谢谢你的帮助</p>