如何使输出循环基于2个字段



我有一个包含字段的数据帧:"nome"acesspoint"dia"momento"纬度;以及";经度";。字段";momento";间隔15分钟。

我需要根据";直径";以及";momento";。

示例:2022年6月3日08:00在DCF,我有两个用户(bruno和thiago(。2022年3月6日08:15在DCF,我有两个用户。2022年3月6日08:00在DCC,我有一个用户(Maria(。

以上示例的输出:

print(weight_list_access)
[-21.22604, -44.97349, 2], [-21.22604, -44.97349, 2], [-21.22780, -44.97850, 1]
#[latitude, longitude, counter]

Dict:

dicionario = {'nome': {0: ' bruno', 1: ' bruno', 2: ' bruno', 3: ' bruno', 4: ' bruno', 5: ' bruno', 6: ' bruno', 7: ' bruno', 8: ' Thiago', 9: ' Thiago', 10: ' Thiago', 11: ' Thiago', 12: ' Thiago', 13: ' Thiago', 14: ' Thiago', 15: ' Thiago', 16: ' Maria', 17: ' Maria', 18: ' Maria', 19: ' Maria', 20: ' Maria', 21: ' Maria', 22: ' Maria', 23: ' Maria', 24: ' Thiago', 25: ' Thiago', 26: ' Thiago', 27: ' Thiago', 28: ' Thiago', 29: ' Thiago', 30: ' Thiago', 31: ' Thiago'}, 'acesspoint': {0: 'DCF', 1: 'DCF', 2: 'DCF', 3: 'DCF', 4: 'DCF', 5: 'DCF', 6: 'DCF', 7: 'DCF', 8: 'DCF', 9: 'DCF', 10: 'DCF', 11: 'DCF', 12: 'DCF', 13: 'DCF', 14: 'DCF', 15: 'DCF', 16: 'DCC', 17: 'DCC', 18: 'DCC', 19: 'DCC', 20: 'DCC', 21: 'DCC', 22: 'DCC', 23: 'DCC', 24: 'DEX', 25: 'DEX', 26: 'DEX', 27: 'DEX', 28: 'DEX', 29: 'DEX', 30: 'DEX', 31: 'DEX'}, 'dia': {0: '03/06/2022', 1: '03/06/2022', 2: '03/06/2022', 3: '03/06/2022', 4: '03/06/2022', 5: '03/06/2022', 6: '03/06/2022', 7: '03/06/2022', 8: '03/06/2022', 9: '03/06/2022', 10: '03/06/2022', 11: '03/06/2022', 12: '03/06/2022', 13: '03/06/2022', 14: '03/06/2022', 15: '03/06/2022', 16: '03/06/2022', 17: '03/06/2022', 18: '03/06/2022', 19: '03/06/2022', 20: '03/06/2022', 21: '03/06/2022', 22: '03/06/2022', 23: '03/06/2022', 24: '04/06/2022', 25: '04/06/2022', 26: '04/06/2022', 27: '04/06/2022', 28: '04/06/2022', 29: '04/06/2022', 30: '04/06/2022', 31: '04/06/2022'}, 'momento': {0: '08:00', 1: '08:30', 2: '08:45', 3: '09:00', 4: '09:15', 5: '09:30', 6: '09:45', 7: '10:00', 8: '08:00', 9: '08:30', 10: '08:45', 11: '09:00', 12: '09:15', 13: '09:30', 14: '09:45', 15: '10:00', 16: '08:00', 17: '08:30', 18: '08:45', 19: '09:00', 20: '09:15', 21: '09:30', 22: '09:45', 23: '10:00', 24: '08:00', 25: '08:30', 26: '08:45', 27: '09:00', 28: '09:15', 29: '09:30', 30: '09:45', 31: '10:00'}, 'Latitude': {0: -21.22604, 1: -21.22604, 2: -21.22604, 3: -21.22604, 4: -21.22604, 5: -21.22604, 6: -21.22604, 7: -21.22604, 8: -21.22604, 9: -21.22604, 10: -21.22604, 11: -21.22604, 12: -21.22604, 13: -21.22604, 14: -21.22604, 15: -21.22604, 16: -21.2278, 17: -21.2278, 18: -21.2278, 19: -21.2278, 20: -21.2278, 21: -21.2278, 22: -21.2278, 23: -21.2278, 24: -21.22707, 25: -21.22707, 26: -21.22707, 27: -21.22707, 28: -21.22707, 29: -21.22707, 30: -21.22707, 31: -21.22707}, 'Longitude': {0: -44.97349, 1: -44.97349, 2: -44.97349, 3: -44.97349, 4: -44.97349, 5: -44.97349, 6: -44.97349, 7: -44.97349, 8: -44.97349, 9: -44.97349, 10: -44.97349, 11: -44.97349, 12: -44.97349, 13: -44.97349, 14: -44.97349, 15: -44.97349, 16: -44.9785, 17: -44.9785, 18: -44.9785, 19: -44.9785, 20: -44.9785, 21: -44.9785, 22: -44.9785, 23: -44.9785, 24: -44.97849, 25: -44.97849, 26: -44.97849, 27: -44.97849, 28: -44.97849, 29: -44.97849, 30: -44.97849, 31: -44.97849}}

我使用了以下代码:

df_acesso = pd.DataFrame(dicionario)
weight_list_access = []
df_acesso['counter'] = 1
for x in df_acesso['dia'].sort_values().unique():
weight_list_access.append(df_acesso.loc[df_acesso['dia'] == x , ['Latitude', "Longitude", "counter"]].groupby(['Latitude',"Longitude"]).sum().reset_index().values.tolist()) 

有了这个代码,你就可以计算一天中的所有连接("dia"(,而不考虑"dia";momento";字段(时间间隔(。我试着用嵌套的for来做这件事;momento";领域但这并没有奏效。

怎么可能呢?

这能满足您的需求吗?

df = pd.DataFrame(dicionario)
df["coords"] = pd.Series(zip(df["Latitude"], df["Longitude"]))
df.groupby(["dia", "momento"])["coords"].value_counts().rename("count").reset_index()

结果

dia momento                  coords  count
0   03/06/2022   08:00  (-21.22604, -44.97349)      2
1   03/06/2022   08:00    (-21.2278, -44.9785)      1
2   03/06/2022   08:30  (-21.22604, -44.97349)      2
3   03/06/2022   08:30    (-21.2278, -44.9785)      1
4   03/06/2022   08:45  (-21.22604, -44.97349)      2
5   03/06/2022   08:45    (-21.2278, -44.9785)      1
6   03/06/2022   09:00  (-21.22604, -44.97349)      2
...

相关内容

  • 没有找到相关文章

最新更新