将列表值中的列表替换为dict值

时间:2019-09-20 08:51:08

标签: python pandas datetime

我的熊猫date_df['col_name']列如下:

[[1, 7]]
[[1, 7]]
[[1, 11], [13, 13]]
[[1, 11], [14, 14], [16, 18]]
[[1, 7]]
[[1, 8], [15, 16]]
[[1, 11]]
[[1, 8], [15, 16]]

其中每个元素都是字典键。 我需要用dict中的值替换列表中列表中的每个元素:

date_dict = 
{0: Timestamp('2017-01-01 00:00:00', freq='MS'),
 1: Timestamp('2017-02-01 00:00:00', freq='MS'),
 2: Timestamp('2017-03-01 00:00:00', freq='MS'),
 3: Timestamp('2017-04-01 00:00:00', freq='MS'),
 4: Timestamp('2017-05-01 00:00:00', freq='MS'),
 5: Timestamp('2017-06-01 00:00:00', freq='MS'),
 ...
 34: Timestamp('2019-11-01 00:00:00', freq='MS'),
 35: Timestamp('2019-12-01 00:00:00', freq='MS')}

我尝试过:

for list_ in date_df['col_name']:
    for idx, item in enumerate(list_):
        if item in date_dict:
            list_[idx] = date_dict[item]

但是此解决方案返回TypeError: unhashable type: 'list'

是否可以用dict值替换列表值?

1 个答案:

答案 0 :(得分:2)

您还需要另一个嵌套列表理解以供内部列表循环:

date_df['col_name'] = date_df['col_name'].apply(lambda x: [[d[z] for z in y] for y in x])

date_df['col_name'] = [[[d[z] for z in y] for y in x] for x in  date_df['col_name']]

如果不匹配,则可以使用.get函数并可能指定默认值:

#some default value
m = pd.Timestamp('2000-01-01 00:00:00', freq='MS')
date_df['col_name']=date_df['col_name'].apply(lambda x: [[d.get(z, m) for z in y] for y in x])

date_df['col_name'] = [[[d.get(z, m) for z in y] for y in x] for x in  date_df['col_name']]

示例

print (date_df)
    col_name
0    [[1,2]]
1  [[100,3]]

d = {0: pd.Timestamp('2017-01-01 00:00:00', freq='MS'),
 1: pd.Timestamp('2017-02-01 00:00:00', freq='MS'),
 2: pd.Timestamp('2017-03-01 00:00:00', freq='MS'),
 3: pd.Timestamp('2017-04-01 00:00:00', freq='MS')}

m = pd.Timestamp('2000-01-01 00:00:00', freq='MS')
date_df['col_name']=date_df['col_name'].apply(lambda x: [[d.get(z,m) for z in y] for y in x])
print (date_df)
                                       col_name
0  [[2017-02-01 00:00:00, 2017-03-01 00:00:00]]
1  [[2000-01-01 00:00:00, 2017-04-01 00:00:00]]