<p>用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.shift.html" rel="nofollow noreferrer">^{<cd1>}</a>就可以简单地做到这一点。问题出现在最后一行,我正在努力找出如何最好地重建。你知道吗</p>
<p>编辑:我在最后一排给了它我最好的机会,但最后它变成了一个笨拙的烂摊子。我很乐意得到关于最后一排的任何反馈。原则上,使用<code>shift</code>会使这变得非常简单。很明显,您可以在添加最后一行之前删除<code>start</code>和<code>end</code>,我只是演示了如何做到不丢失数据。你知道吗</p>
<pre><code>import pandas as pd
import numpy as np
import datetime as dt
df = pd.DataFrame({'date': [dt.date(2019, 4, 4), dt.date(2019, 4, 5), dt.date(2019, 4, 5)],
'start': [pd.Timestamp(2019, 4, 4, 10), pd.Timestamp(2019, 4, 5, 0), pd.Timestamp(2019, 4, 5, 14)],
'end': [pd.Timestamp(2019, 4, 4, 16), pd.Timestamp(2019, 4, 5, 4), pd.Timestamp(2019, 4, 5, 18)]})
df = df[['date', 'start', 'end']]
saved_shift_ending = df['end'].iloc[-1] # we want end of last shift
saved_end_date = df['date'].iloc[-1] # we also want the date value
start_date = df['date'].min()
end_date = (df['date'].max() + dt.timedelta(days=1))
df['other_start'] = df['end'].shift(1)
df['other_end'] = df['start']
df.loc[0, 'other_start'] = start_date # The first value is NaT after shift
last_row = pd.DataFrame([[saved_end_date.strftime('%Y-%m-%d'),
np.nan,
np.nan,
saved_shift_ending,
end_date]],
columns=['date', 'start', 'end', 'other_start',
'other_end'])
df = df.append(last_row)
df.drop(['start', 'end'], axis=1, inplace=True)
print(df)
</code></pre>