【发布时间】:2022-12-15 15:32:32
【问题描述】:
我有一个数据整理任务,我想以某种格式保存输入。当我感到疲倦时,我决定尝试使用 OpenAI,看看代码生成能做什么。它得出了与我最初追求的不同的解决方案。如果这是一件好事,我仍然有点犹豫,特别是对于那些刚刚学习编码的人,但这是一个不同的主题。
迅速的:
create a python function to filter pandas dataframe values by a seperate dictionary with tuples as the keys
for example:
I have a dataframe named "temp_obs" like pd.DataFrame([[2000, January, 1,2,3],[2000, March,3,3],[2004, December,2,6,1,2] ,[1993, December,2]]
I have a dictionary named "phase" with keys as tuples corresponding to the dataframe columns and rows, like this {(2000,January):0, (2000,March):3, (2004,December):3, (1993,December):0, (1999,March):5 ,(2020,January):1}.
I want to filter the dataframe such that I only get the rows with the corresponding dictionary values greater than 2.
the output would be a dataframe with rows [2000, March,3,3] and [2004, December,2,6,1,2]
【问题讨论】: