为列组合填充缺少的日期

2024-04-26 09:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框,3列,一个日期,2个对象列。我需要使用数据帧的最大和最小日期来填充不同列1和列2组合的缺失日期。日期列只包含每个月的第一天。你知道吗

我已经用天真的方式做了,但原始数据是在数千或记录需要大量的时间来迭代通过所有COL1+COL2组合,日期范围。原始数据帧包含15000条记录和30列。我需要填充缺少的date+col1+col2,然后将所有列都保留为空值。如果我有2019年1月的col1+col2组合的数据,而没有2月的数据,我实际上想把feb,col1,col2,其他记录插入空。你知道吗

从原始数据帧到填充后,应该有相等的唯一组合(COL1+COL2)。前后组合次数相同

请帮我优化一下。你知道吗

df_1 = pd.DataFrame({'Date':['2018-01-01','2018-02-01','2018-03-01','2018-05-01','2018-05-01'],
          'COL1':['A','A','B','B','A'],
          'COL2':['1','2','1','2','1']})
df_1['Date'] = pd.to_datetime(df_1['Date'])



Initial Dataframe -->> 
         Date COL1 COL2
0 2018-01-01    A    1
1 2018-02-01    A    2
2 2018-03-01    B    1
3 2018-05-01    B    2
4 2018-05-01    A    1

---

print(df_1.dtypes)

print(df_1)

COLS_COMBO = [i for i in list(set(list(df_1[['COL1','COL2']].itertuples(name='',index=False))))]
months_range = [str(i.date()) for i in list(pd.date_range(start=min(df_1['Date']).date(), 
                   end=max(df_1['Date']).date(), freq='MS'))]

print(COLS_COMBO)

print(months_range)

for col in COLS_COMBO:
    col1,col2 = col[0], col[1]
    for month in months_range:
        d = df_1[(df_1['Date'] == month) & (df_1['COL1'] == col1) & (df_1['COL2'] == col2)]
        if len(d) == 0:
            dx = {'Date':month,'COL1':col1,'COL2':col2}
            df_1 = df_1.append(dx, ignore_index=True)

print(df_1)

输出

Data TYPES -->> 


Date    datetime64[ns]
COL1            object
COL2            object
dtype: object


Unique COmbinations of COL1 + COL2 -->> 
 [('A', '2'), ('B', '2'), ('B', '1'), ('A', '1')]

Months range using min, max in the dataframe -->> 
 ['2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01', '2018-05-01']

我的最终结果是

FINAL Dataframe -->> 

          Date COL1 COL2
0  2018-01-01    A    1
1  2018-02-01    A    2
2  2018-03-01    B    1
3  2018-05-01    B    2
4  2018-05-01    A    1
5  2018-01-01    A    2
6  2018-02-01    A    2
7  2018-03-01    A    2
8  2018-04-01    A    2
9  2018-05-01    A    2
10 2018-01-01    B    2
11 2018-02-01    B    2
12 2018-03-01    B    2
13 2018-04-01    B    2
14 2018-05-01    B    2
15 2018-01-01    B    1
16 2018-02-01    B    1
17 2018-03-01    B    1
18 2018-04-01    B    1
19 2018-05-01    B    1
20 2018-01-01    A    1
21 2018-02-01    A    1
22 2018-03-01    A    1
23 2018-04-01    A    1
24 2018-05-01    A    1

附言:

COL1就像父COL2是子COL2一样。因此,原始的组合应该没有变化,而且(date+col1+col2)组合不应该被复制/更新(如果存在的话)。你知道吗


Tags: 数据indffor原始数据date记录range
1条回答
网友
1楼 · 发布于 2024-04-26 09:23:07

您可以使用:

from  itertools import product

#get all unique combinations of columns
COLS_COMBO = df_1[['COL1','COL2']].drop_duplicates().values.tolist()
#remove times and create MS date range
dates = df_1['Date'].dt.floor('d')
months_range = pd.date_range(dates.min(), dates.max(), freq='MS')
print(COLS_COMBO)
print(months_range)

#create all combinations of values
df = pd.DataFrame([(c, a, b) for (a, b), c in product(COLS_COMBO, months_range)], 
                   columns=['Date','COL1','COL2'])
print (df)
         Date COL1 COL2
0  2018-01-01    A    1
1  2018-02-01    A    1
2  2018-03-01    A    1
3  2018-04-01    A    1
4  2018-05-01    A    1
5  2018-01-01    A    2
6  2018-02-01    A    2
7  2018-03-01    A    2
8  2018-04-01    A    2
9  2018-05-01    A    2
10 2018-01-01    B    1
11 2018-02-01    B    1
12 2018-03-01    B    1
13 2018-04-01    B    1
14 2018-05-01    B    1
15 2018-01-01    B    2
16 2018-02-01    B    2
17 2018-03-01    B    2
18 2018-04-01    B    2
19 2018-05-01    B    2

#add to original df_1 and remove duplicates
df_1 = pd.concat([df_1, df], ignore_index=True).drop_duplicates()
print (df_1)
         Date COL1 COL2
0  2018-01-01    A    1
1  2018-02-01    A    2
2  2018-03-01    B    1
3  2018-05-01    B    2
4  2018-05-01    A    1
6  2018-02-01    A    1
7  2018-03-01    A    1
8  2018-04-01    A    1
10 2018-01-01    A    2
12 2018-03-01    A    2
13 2018-04-01    A    2
14 2018-05-01    A    2
15 2018-01-01    B    1
16 2018-02-01    B    1
18 2018-04-01    B    1
19 2018-05-01    B    1
20 2018-01-01    B    2
21 2018-02-01    B    2
22 2018-03-01    B    2
23 2018-04-01    B    2

相关问题 更多 >