我有一个pandas数据框,其中一列为日期
2015-11-01
2015-12-01
2016-01-01
2016-03-01
2016-03-01
2016-10-01
2016-10-01
2016-12-01
2017-03-01
我想插入两行, 1.在第一行的前一个月开头的一行 2.最后一行下个月末的一行
获得所需的输出,
2015-10-01
2015-11-01
2015-12-01
2016-01-01
2016-03-01
2016-03-01
2016-10-01
2016-10-01
2016-12-01
2017-03-01
2017-04-01
这样做的pythonic方式是什么?
答案 0 :(得分:2)
尝试MonthBegin:
import pandas as pd
df=pd.DataFrame(['2015-11-01','2015-12-01','2016-01-01','2016-03-01','2016-03-01','2016-10-01','2016-10-01','2016-12-01','2017-03-01'],columns=['date'])
df['date']=pd.to_datetime(df['date'])
df=pd.DataFrame([df.loc[0,'date'] - pd.offsets.MonthBegin(1)]+list(df['date'])+[df.loc[len(df)-1,'date'] + pd.offsets.MonthBegin(1)],columns=['date'])
df
输出:
date
0 2015-10-01
1 2015-11-01
2 2015-12-01
3 2016-01-01
4 2016-03-01
5 2016-03-01
6 2016-10-01
7 2016-10-01
8 2016-12-01
9 2017-03-01
10 2017-04-01
答案 1 :(得分:1)
使用:
df['date']=pd.to_datetime(df['date'])
a = df.loc[1, 'date'] - pd.offsets.MonthBegin()
b = df.loc[len(df.index) - 1, 'date'] + pd.offsets.MonthBegin()
df = pd.DataFrame([a] + df['date'].tolist() + [b], columns=['date'])
print (df)
date
0 2015-11-01
1 2015-11-01
2 2015-12-01
3 2016-01-01
4 2016-03-01
5 2016-03-01
6 2016-10-01
7 2016-10-01
8 2016-12-01
9 2017-03-01
10 2017-04-01
或者:
df.index = df.index + 1
df.loc[0, 'date'] = df.loc[1, 'date'] - pd.offsets.MonthBegin()
df.loc[len(df.index), 'date'] = df.loc[len(df.index) - 1, 'date'] + pd.offsets.MonthBegin()
df = df.sort_index()
print (df)
date
0 2015-10-01
1 2015-11-01
2 2015-12-01
3 2016-01-01
4 2016-03-01
5 2016-03-01
6 2016-10-01
7 2016-10-01
8 2016-12-01
9 2017-03-01
10 2017-04-01