Python 3.x 更改列表中每个字典中特定键的值-python

Python 3.x 更改列表中每个字典中特定键的值-python,python-3.x,pandas,list,dataframe,dictionary,Python 3.x,Pandas,List,Dataframe,Dictionary,我有一个字典列表,如下所示 [{"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.

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

[{"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”值

最后一个字典的“to”值保持原样

预期产出:

[{"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]
          }]

记录
(词典列表)创建一个新的数据框,然后在
列+上使用
列,并将其分配回
列,下一步使用以获取词典列表:

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]}]

创建了一个新问题,请在空闲时间研究