向量化实现可从pandas数据框中的单行创建多行 [英] Vectorized implementation to create multiple rows from a single row in pandas dataframe
问题描述
对于输入表中的每一行,我需要通过基于每月分隔日期范围来生成多行. (请参考下面的示例输出).
For each row in the input table, I need to generate multiple rows by separating the date range based on monthly. (please refer to the below sample output).
有一种简单的迭代方法可以逐行转换,但是在大型数据帧上速度很慢.
There is a simple iterative approach to convert row by row, but it is very slow on large dataframes.
有人可以建议采用矢量化方法,例如使用apply(),map()等实现目标吗?
Could anyone suggest a vectorized approach, such as using apply(), map() etc. to achieve the objective?
输出表是一个新表.
输入:
ID, START_DATE, END_DATE
1, 2010-12-08, 2011-03-01
2, 2010-12-10, 2011-01-12
3, 2010-12-16, 2011-03-07
输出:
ID, START_DATE, END_DATE, NUMBER_DAYS, ACTION_DATE
1, 2010-12-08, 2010-12-31, 23, 201012
1, 2010-12-08, 2011-01-31, 54, 201101
1, 2010-12-08, 2011-02-28, 82, 201102
1, 2010-12-08, 2011-03-01, 83, 201103
2, 2010-12-10, 2010-12-31, 21, 201012
2, 2010-12-10, 2011-01-12, 33, 201101
3, 2010-12-16, 2010-12-31, 15, 201012
4, 2010-12-16, 2011-01-31, 46, 201101
5, 2010-12-16, 2011-02-28, 74, 201102
6, 2010-12-16, 2011-03-07, 81, 201103
推荐答案
我认为您可以使用:
import pandas as pd
df = pd.DataFrame({'ID': {0: 1, 1: 2, 2: 3},
'END_DATE': {0: pd.Timestamp('2011-03-01 00:00:00'),
1: pd.Timestamp('2011-01-12 00:00:00'),
2: pd.Timestamp('2011-03-07 00:00:00')},
'START_DATE': {0: pd.Timestamp('2010-12-08 00:00:00'),
1: pd.Timestamp('2010-12-10 00:00:00'),
2: pd.Timestamp('2010-12-16 00:00:00')}},
columns=['ID','START_DATE', 'END_DATE'])
print df
ID START_DATE END_DATE
0 1 2010-12-08 2011-03-01
1 2 2010-12-10 2011-01-12
2 3 2010-12-16 2011-03-07
#if multiple columns, you can filter them by subset
#df = df[['ID','START_DATE', 'END_DATE']]
#stack columns START_DATE and END_DATE
df1 = df.set_index('ID')
.stack()
.reset_index(level=1, drop=True)
.to_frame()
.rename(columns={0:'Date'})
#print df1
#resample and fill missing data
df1 = df1.groupby(df1.index).apply(lambda x: x.set_index('Date').resample('M').asfreq())
.reset_index()
print df1
ID Date
0 1 2010-12-31
1 1 2011-01-31
2 1 2011-02-28
3 1 2011-03-31
4 2 2010-12-31
5 2 2011-01-31
6 3 2010-12-31
7 3 2011-01-31
8 3 2011-02-28
9 3 2011-03-31
Month
的最后一天存在问题,因为resample
添加了Month
的最后一天,所以首先创建period
列,然后创建 combine_first
从列中添加缺失值Date
并通过 bfill
添加START_DATE
列的值缺失.
There is problem with last day of Month
, because resample
add last day of Month
, so first create period
columns and then merge
them. By combine_first
add missing values from column Date
and by bfill
add missing values of column START_DATE
.
df['period'] = df.END_DATE.dt.to_period('M')
df1['period'] = df1.Date.dt.to_period('M')
df2 = pd.merge(df1, df, on=['ID','period'], how='left')
df2['END_DATE'] = df2.END_DATE.combine_first(df2.Date)
df2['START_DATE'] = df2.START_DATE.bfill()
df2 = df2.drop(['Date','period'], axis=1)
最后通过与 dt.days
和 dt.strftime
:
Last add new columns by difference with dt.days
and dt.strftime
:
df2['NUMBER_DAYS'] = (df2.END_DATE - df2.START_DATE).dt.days
df2['ACTION_DATE'] = df2.END_DATE.dt.strftime('%Y%m')
print df2
ID START_DATE END_DATE NUMBER_DAYS ACTION_DATE
0 1 2010-12-08 2010-12-31 23 201012
1 1 2010-12-08 2011-01-31 54 201101
2 1 2010-12-08 2011-02-28 82 201102
3 1 2010-12-08 2011-03-01 83 201103
4 2 2010-12-10 2010-12-31 21 201012
5 2 2010-12-10 2011-01-12 33 201101
6 3 2010-12-16 2010-12-31 15 201012
7 3 2010-12-16 2011-01-31 46 201101
8 3 2010-12-16 2011-02-28 74 201102
9 3 2010-12-16 2011-03-07 81 201103
这篇关于向量化实现可从pandas数据框中的单行创建多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!