问题描述
我有一个包含三列 Device
Date
和 Level
的 3643 行数据框。它的结构使得无论我们有多少个级别的日期,Device
都会在列中重复,例如:
Device Date Level
NY 2018-01-02 53
NY 2018-01-03 40
NY 2018-01-04 48
NY 2018-01-05 43
NY 2018-01-06 47
LA 2018-01-02 52
LA 2018-01-05 39
LA 2018-01-07 55
CHI 2018-03-01 73
CHI 2018-03-04 92
CHI 2018-03-05 54
CHI 2018-03-07 29
我已成功过滤掉低于 18 的级别,并将此类过滤器应用于我的数据帧以进行预处理。过滤后,我可以根据设备对它们进行日期和级别分组。
我正在努力创建一个字典,其中对于每个设备,我都有日期和级别的配对输出。我曾尝试使用 defaultdict,但我的日期输出为 datetime.date(2018,1,2),53
而不是 2018-01-02,53
。我确实将我的日期列转换为日期时间以便正确过滤我的数据,但我不知道如何将其转换回来以便我的输出是:
'NY':[(2018-01-02,53),(2018-01-03,40),(2018-01-04,48),(2018-01-05,43),(2018-01-06,47)],'LA':[(2018-01-02,52)....
]
这是我迄今为止尝试过的,包括过滤广告分组:
xls = pd.ExcelFile('./TrimmedData_2016-2021.xlsx',engine= 'openpyxl')
df = pd.read_excel(xls,'2018')
df['Date'] = df.apply(lambda row: pd.to_datetime(row['Date']).date(),axis=1)
grouped=df.groupby([df['Device'],df['Date']])['Date'].count()
grouped_set = set(grouped[grouped > 18].index.tolist())
df["filter"] = df.apply(lambda row: ((row['Device'],row['Date']) in grouped_set),axis=1)
df = df[df["filter"]]
df_grouped = df.groupby([df['Device'],df['Date']])['Level'].mean().reset_index()
#here is where i create the dictionary and things look strange
from collections import defaultdict
res = defaultdict(list)
for index,row in tqdm(df_grouped.iterrows(),position=0,leave=True):
res[row["Device"]].append((row["Date"],round(row["Level"],2)))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)