擅长:python、mysql、java
<p>您需要在这里使用<code>df.shift</code>。<br/>
<code>df.shift(i)</code>将整个数据帧向下移动<code>i</code>个单位。</p>
<p>因此,对于<code>i = 1</code>:</p>
<p>输入:</p>
<pre><code> x1 x2
0 206 214
1 226 234
2 245 253
3 265 272
4 283 291
</code></pre>
<p>输出:</p>
<pre><code> x1 x2
0 Nan Nan
1 206 214
2 226 234
3 245 253
4 265 272
</code></pre>
<p>因此,运行此脚本以获得预期的输出:</p>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
df = pd.DataFrame({'x1': ['206', '226', '245',' 265', '283'],
'x2': ['214', '234', '253', '272', '291']})
print(df)
df['x2'] = df['x2'].shift(1)
print(df)
</code></pre>