【问题标题】:Sort the list of dictionaries based on date column of dataframe in pandas根据熊猫中数据框的日期列对字典列表进行排序
【发布时间】:2020-11-06 12:49:10
【问题描述】:

我有一个输入列表和数据框,如下所示。

[{"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": "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": "polynomial",
  "from": "2020-02-05T20: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]
  }]

df:

Date                t_factor     
2020-02-01             5             
2020-02-02             23              
2020-02-03             14           
2020-02-04             23
2020-02-05             23  
2020-02-06             23          
2020-02-07             30            
2020-02-08             29            
2020-02-09             100
2020-03-10             38
2020-03-11             38               
2020-03-12             38                    
2020-03-13             70           
2020-03-14             70 

Step1:根据字典中“from”键的值对列表进行排序

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

Step2:添加一个值为“from”键的字典作为df的最小日期,“to”应该是“from”日期排序列表中的第一个字典。 “天” = 0,“系数”:[0.1,0.1,0.1,0.1,0.1,0.1]。

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

Step3:添加一个字典,其值为“from”键为 df 最小日期后 7 天,“to”应为 from 后 1 天

{"type": "df_mid",
      "from": "2020-02-08T20:00:00.000Z",
      "to": "2020-02-09T20:00:00.000Z",
      "days":0,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      }

Step4:添加一个字典,其值为“from”键作为df的最大日期,“to”应该与“from”相同。

{"type": "df_last",
      "from": "2020-02-14T20:00:00.000Z",
      "to": "2020-02-14T20:00:00.000Z",
      "days":0,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      }

第 5 步:根据“开始”日期对所有字典进行排序。

预期输出:

[{"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-05T20: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": "df_mid",
      "from": "2020-02-08T20:00:00.000Z",
      "to": "2020-02-09T20:00:00.000Z",
      "days":0,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      },

{"type": "df_last",
      "from": "2020-02-14T20:00:00.000Z",
      "to": "2020-02-14T20:00:00.000Z",
      "days":0,
      "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
      }
]

第 6 步:

将每个字典的“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-05T20:00:00.000Z",
          "days":3,
          "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          },
    
         {"type": "polynomial",
          "from": "2020-02-05T20: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": "df_mid",
          "from": "2020-02-08T20:00:00.000Z",
          "to": "2020-02-14T20:00:00.000Z",
          "days":0,
          "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          },
    
    {"type": "df_last",
          "from": "2020-02-14T20:00:00.000Z",
          "to": "2020-02-14T20:00:00.000Z",
          "days":0,
          "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
          }
    ]

【问题讨论】:

  • 收到你的通知,你把我加了标签,我会看看这个问题..
  • @Shubham Sharma 非常感谢

标签: python-3.x pandas dataframe


【解决方案1】:

定义一个函数add_dct,它将参数作为字典列表lst_type_from_to,并将一个新字典附加到lst

dmin, dmax = df['Date'].min(), df['Date'].max()
def add_dct(lst, _type, _from, _to):
    lst.append({
        'type': _type,
        'from': _from if isinstance(_from, str) else _from.strftime("%Y-%m-%dT20:%M:%S.000Z"),
        'to': _to if isinstance(_to, str) else _to.strftime("%Y-%m-%dT20:%M:%S.000Z"),
        'days': 0,
        "coef":[0.1,0.1,0.1,0.1,0.1,0.1]
    })

根据您的predefined 要求执行以下步骤:

# STEP 1
lst = sorted(lst, key=lambda d: pd.Timestamp(d['from']))

# STEP 2
add_dct(lst, 'df_first', dmin, lst[0]['from'])

# STEP 3
add_dct(lst, 'df_mid', dmin + pd.Timedelta(days=7), dmin + pd.Timedelta(days=8))

# STEP 4
add_dct(lst, 'df_last', dmax, dmax)

# STEP 5
lst = sorted(lst, key=lambda d: pd.Timestamp(d['from']))

结果:

[{'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-05T20: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': 'df_mid',
  'from': '2020-02-08T20:00:00.000Z',
  'to': '2020-02-09T20:00:00.000Z',
  'days': 0,
  'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
 {'type': 'df_last',
  'from': '2020-03-14T20:00:00.000Z',
  'to': '2020-03-14T20:00:00.000Z',
  'days': 0,
  'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]}]

【讨论】:

  • 非常感谢..你是天才。愿上帝保佑你
  • 最后我想将“to”值替换为下一个字典的“from”值。你能帮我解决这个问题吗?
  • 你能详细解释一下吗?
  • 添加了第6步和预期的输出,只是将“to”替换为下一个字典的“from”
  • @Danish 你能创建一个新的格式良好的问题吗,第 6 步似乎可以在完全其他问题中制定?只需在问题中给出示例字典,并询问如何将 to 替换为 from?
猜你喜欢
  • 2014-12-29
  • 2015-11-06
  • 2021-10-07
  • 2018-07-08
  • 2012-06-19
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
相关资源
最近更新 更多