使用字典值创建新列

Create new columns with dictionary values

import pandas as pd

data = [['2018', '2018-03-25 01:00:00', '2018-10-28 02:00:00'], 
        ['2019', '2019-03-31 01:00:00', '2019-10-27 02:00:00'], 
        ['2020', '2020-03-29 01:00:00', '2020-10-25 02:00:00'], 
        ['2021', '2021-03-28 01:00:00', '2021-10-31 02:00:00']]
    
df = pd.DataFrame(data, columns = ['Year', 'DST Start', 'DST End'])
df_dict = df.set_index('Year').T.to_dict('list')


data = ['2018','2019', '2019', '2019', '2019', '2020', '2020','2020', '2021']
    
df1 = pd.DataFrame(data, columns = ['date'])

我正在尝试使用 df date 列中的值针对 df_dict 中的 keys 执行查找方法(因为需要更好的词)来创建新的df1 中的列如下:

   date                 col1                 col2
0  2018  2018-03-25 01:00:00  2018-10-28 02:00:00
1  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
2  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
3  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
4  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
5  2020  2020-03-29 01:00:00  2020-10-25 02:00:00
6  2020  2020-03-29 01:00:00  2020-10-25 02:00:00
7  2020  2020-03-29 01:00:00  2020-10-25 02:00:00
8  2021  2021-03-28 01:00:00  2021-10-31 02:00:00

然而,我能得到的最接近的是如下所示:

df1['col1'] = df1['date'].map(df_dict)
print(df1)


   date                                        col1
0  2018  [2018-03-25 01:00:00, 2018-10-28 02:00:00]
1  2019  [2019-03-31 01:00:00, 2019-10-27 02:00:00]
2  2019  [2019-03-31 01:00:00, 2019-10-27 02:00:00]
3  2019  [2019-03-31 01:00:00, 2019-10-27 02:00:00]
4  2019  [2019-03-31 01:00:00, 2019-10-27 02:00:00]
5  2020  [2020-03-29 01:00:00, 2020-10-25 02:00:00]
6  2020  [2020-03-29 01:00:00, 2020-10-25 02:00:00]
7  2020  [2020-03-29 01:00:00, 2020-10-25 02:00:00]
8  2021  [2021-03-28 01:00:00, 2021-10-31 02:00:00]

如果我清楚地理解你的挑战,这就是合并:

(df.rename(columns={'DST Start' : 'col1', 'DST End': 'col2'})
   .merge(df1.rename(columns={'date':'Year'}), 
          on='Year')
 )
 
   Year                 col1                 col2
0  2018  2018-03-25 01:00:00  2018-10-28 02:00:00
1  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
2  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
3  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
4  2019  2019-03-31 01:00:00  2019-10-27 02:00:00
5  2020  2020-03-29 01:00:00  2020-10-25 02:00:00
6  2020  2020-03-29 01:00:00  2020-10-25 02:00:00
7  2020  2020-03-29 01:00:00  2020-10-25 02:00:00
8  2021  2021-03-28 01:00:00  2021-10-31 02:00:00