如何使用 pandas 的开始日期和结束日期提取DataFrame [英] How to extract a DataFrame using start and end dates with Pandas
本文介绍了如何使用 pandas 的开始日期和结束日期提取DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何使用开始日期和结束日期提取DataFrame
并实现此输出?
How can we extract the DataFrame
using start and end dates and achieve this output?
输入
id start end
1 2009 2014
2 2010 2012
输出
id data
1 2009
1 2010
1 2011
1 2012
1 2013
1 2014
2 2010
2 2011
2 2012
推荐答案
使用:
df1 = (pd.concat([pd.Series(r.id,np.arange(r.start, r.end + 1)) for r in df.itertuples()])
.reset_index())
df1.columns = ['data','id']
df1 = df1[['id','data']]
print (df1)
id data
0 1 2009
1 1 2010
2 1 2011
3 1 2012
4 1 2013
5 1 2014
6 2 2010
7 2 2011
8 2 2012
这篇关于如何使用 pandas 的开始日期和结束日期提取DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文