我有一个 DataFrame,它看起来像:
col_1 col_2 ... col_n date
1 1 0 1 [[2017-02-01, 2017-12-01]]
2 0 1 1 [[2018-01-01, 2018-01-01], [2019-01-01, 2019-02-01]]
3 1 1 0 [[2018-04-01, 2019-03-01]]
...
n 0 0 1 [[2017-12-01, 2017-12-01], [2018-03-01, 2018-03-01], [2018-05-01, 2018-05-01], [2018-08-01, 2018-12-01]]
而且我需要重复df.date具有多个列表值的列并将它们拆分为新列df.start_date和df.end_date
例如
col_1 col_2 ... col_n date_start date_end
1 1 0 1 2017-02-01 2017-12-01
2 0 1 1 2018-01-01 2018-01-01
3 0 1 1 2019-01-01 2019-02-01
4 1 1 0 2018-04-01 2019-03-01
...
n 0 0 1 2017-12-01 2017-12-01
n 0 0 1 2018-03-01 2018-03-01
n 0 0 1 2018-05-01 2018-05-01
n 0 0 1 2018-08-01 2018-12-01
我试过了
date_df['repeat_num'] = [[[row, idx] for idx, item in enumerate(_list)] for row, _list in enumerate(date_df['date'])]
for row in range(len(date_df)):
if id_tuple[row][0][1] == 1: np.repeat(date_df.values, 1, axis = 0)
elif id_tuple[row][0][1] == 2: np.repeat(date_df.values, 2, axis = 0)
elif id_tuple[row][0][1] == 3: np.repeat(date_df.values, 3, axis = 0)
elif id_tuple[row][0][1] == 4: np.repeat(date_df.values, 4, axis = 0)
elif id_tuple[row][0][1] == 5: np.repeat(date_df.values, 5, axis = 0)
但不要认为它工作正常。
有没有办法做到这一点?
慕容708150
相关分类