我正在处理一个包含多个日期列的巨大数据帧。以下是一个示例:
import pandas as pd
import numpy as np
rng = pd.date_range('2015-02-24', periods=3)
rng2 = pd.date_range('2015-02-25', periods=3)
df = pd.DataFrame({ 'Arrive': rng, 'Dept': rng2, 'Val' : np.random.randn(len(rng))})
print(df)
Arrive Dept Val
0 2015-02-24 2015-02-25 -1.576528
1 2015-02-25 2015-02-26 0.803651
2 2015-02-26 2015-02-27 0.166160
现在我使用以下方法复制行两次:
dupli_df = pd.concat([df]*3, ignore_index=True)
print(dupli_df)
Arrive Dept Val
0 2015-02-24 2015-02-25 -1.576528
1 2015-02-25 2015-02-26 0.803651
2 2015-02-26 2015-02-27 0.166160
3 2015-02-24 2015-02-25 -1.576528
4 2015-02-25 2015-02-26 0.803651
5 2015-02-26 2015-02-27 0.166160
6 2015-02-24 2015-02-25 -1.576528
7 2015-02-25 2015-02-26 0.803651
8 2015-02-26 2015-02-27 0.166160
我要做的是为其中一个重复行的df['Arrive']
和df['Dept']
加上一天,为另一个重复行的两列减去一天。所以基本上,我想得到这样一个数据帧:
Arrive Dept Val
0 2015-02-24 2015-02-25 -1.576528
1 2015-02-25 2015-02-26 0.803651
2 2015-02-26 2015-02-27 0.166160
3 2015-02-25 2015-02-26 -1.576528
4 2015-02-26 2015-02-27 0.803651
5 2015-02-27 2015-02-28 0.166160
6 2015-02-23 2015-02-24 -1.576528
7 2015-02-24 2015-02-25 0.803651
8 2015-02-25 2015-02-26 0.166160
我想创建两个独立的数据帧并将它们连接在一起,但我不确定这是否是最有效的方法。你知道吗
提前谢谢你的建议。你知道吗
可以
concat
,键是以天为单位的偏移量。然后我们加上。你知道吗您可以在} ,例如:
concat
之后对dupli_df
的右侧部分进行切片,并使用^{相关问题 更多 >
编程相关推荐