更改列表中每个字典的特定键的值-python [英] Change the value of a particular key each dictionary in a list - python

查看:69
本文介绍了更改列表中每个字典的特定键的值-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典列表,如下所示。

I have a list of dictionary as shown below.

[{"type": "df_first",
      "from": "2020-02-01T20:00:00.000Z",
      "to": "2020-02-03T20:00:00.000Z",
      "days":0,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      },
 {"type": "quadratic",
  "from": "2020-02-03T20:00:00.000Z",
  "to": "2020-02-10T20:00:00.000Z",
  "days":3,
  "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      },
{"type": "linear",
      "from": "2020-02-04T20:00:00.000Z",
      "to": "2020-02-03T20:00:00.000Z",
      "days":3,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      },
{"type": "polynomial",
 "from": "2020-02-08T20:00:00.000Z",
 "to": "2020-02-08T20:00:00.000Z",
 "days":3,
 "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      }
]

在以上词典中,我要替换 to为每个字典的值作为 from。

From the above dictionary I would like to replace "to" value of each dictionary as the "from" value of the next dictionary.

to表示最后一个字典的值应保持不变

The "to" value of last dictionary be as it is

预期输出:

[{"type": "df_first",
          "from": "2020-02-01T20:00:00.000Z",
          "to": "2020-02-03T20:00:00.000Z",
          "days":0,
          "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          },
     {"type": "quadratic",
      "from": "2020-02-03T20:00:00.000Z",
      "to": "2020-02-04T20:00:00.000Z",
      "days":3,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          },
    {"type": "linear",
          "from": "2020-02-04T20:00:00.000Z",
          "to": "2020-02-08T20:00:00.000Z",
          "days":3,
          "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          },
    {"type": "polynomial",
     "from": "2020-02-08T20:00:00.000Z",
     "to": "2020-02-08T20:00:00.000Z",
     "days":3,
     "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          }]


推荐答案

记录(词典列表)创建新的数据框),然后使用 来自列的上的Series.shift + Series.fillna to 列并将其分配回列,接下来使用 DataFrame.to_dict 以获得字典列表:

Create a new dataframe from records (list of dictionaries), then use Series.shift on from column + Series.fillna with tocolumn and assign it back to to column, next use DataFrame.to_dict to get list of dictionaries:

df = pd.DataFrame(records)
df['to'] = df['from'].shift(-1).fillna(df['to'])
records = df.to_dict('r')

结果:

# print(records)

[{'type': 'df_first',
  'from': '2020-02-01T20:00:00.000Z',
  'to': '2020-02-03T20:00:00.000Z',
  'days': 0,
  'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
 {'type': 'quadratic',
  'from': '2020-02-03T20:00:00.000Z',
  'to': '2020-02-04T20:00:00.000Z',
  'days': 3,
  'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
 {'type': 'linear',
  'from': '2020-02-04T20:00:00.000Z',
  'to': '2020-02-08T20:00:00.000Z',
  'days': 3,
  'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
 {'type': 'polynomial',
  'from': '2020-02-08T20:00:00.000Z',
  'to': '2020-02-08T20:00:00.000Z',
  'days': 3,
  'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]}]

这篇关于更改列表中每个字典的特定键的值-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆