Matheus, I’ve assembled a code where I create a dictionary with the data that Voce passed there, and create 2 methods: one to add the exact week of the year on each object of the user list, another to organize the output table, to be as close as possible to what you want to mount there in your example.
To identify the week, I used the attribute .isocalendar(), that returns a tuple containing the calendar of the week.
import datetime
user_list = [
{
"name": "Talita",
"count": 4,
"date": datetime.date(year=2017, month=3, day=6),
},
{
"name": "Filho",
"count": 8,
"date": datetime.date(year=2017, month=3, day=7),
},
{
"name": "Jao",
"count": 10,
"date": datetime.date(year=2017, month=3, day=7),
},
{
"name": "Otavio",
"count": 6,
"date": datetime.date(year=2017, month=3, day=7),
},
{
"name": "Gabriel",
"count": 2,
"date": datetime.date(year=2017, month=3, day=8),
},
{
"name": "Guilherme",
"count": 1,
"date": datetime.date(year=2017, month=3, day=13),
},
]
def get_week(user_list):
for u in user_list:
week = u['date'].isocalendar()[1]
u['week'] = week
return user_list
def group_by_week(user_list):
actual_week = 0
week_group = {}
for u in user_list:
if actual_week != u['week']:
week_group[u['week']] = []
week_group[u['week']].append(u)
actual_week = u['week']
else:
week_group[actual_week].append(u)
return week_group
# Adicionando semana em cada objeto
print("~> Adicionando semana em cada objeto e printando")
user_list = get_week(user_list)
for t in user_list:
print("Name: %s, week: %s" % (t['name'], t['week']))
print("\n~> Organizando tabela de saida")
# Organizando tabela
week_group = group_by_week(user_list)
count_week = 0
for key in week_group:
if count_week < key:
count_week += 1
print("# Semana %s" % count_week)
for user in week_group[key]:
print("Name: %s, count: %s, date: %s" % (user['name'], user['count'], user['date']))
Upshot
~> Adicionando semana em cada objeto e printando
Name: Talita, week: 10
Name: Filho, week: 10
Name: Jao, week: 10
Name: Otavio, week: 10
Name: Gabriel, week: 10
Name: Guilherme, week: 11
~> Organizando tabela de saida
# Semana 1
Name: Talita, count: 4, date: 2017-03-06
Name: Filho, count: 8, date: 2017-03-07
Name: Jao, count: 10, date: 2017-03-07
Name: Otavio, count: 6, date: 2017-03-07
Name: Gabriel, count: 2, date: 2017-03-08
# Semana 2
Name: Guilherme, count: 1, date: 2017-03-13
What would group by weeks? Why is it necessary to check how many weeks there are between the dates? Where does this table come from?
– Woss
Group when Count the name had in the week of that date.. I had thought to check how many weeks have between the day 06 the day 08 example for a longer date would have 7 weeks with this I will check this date 2017-03-06 belongs in the first week so I will put in the first week Alita had 4 Count.. On the table I set up to learn how to group this data per week. Sorry I don’t have any information
– Matheus Francisco
It might be me, and if it is, I’m sorry, but I just couldn’t understand what you said. "Group when Count name had in the week of that date", that sentence didn’t seem to make sense; "check how many weeks have between the day 06 the day 08 example for a longer date would have 7 weeks", what? Between 06 and 08 only 1 day ago and what would be this longer date? Is it possible to put a [mcve]? Mostly with dates that are weeks apart, not just next dates.
– Woss
I’m sorry, but I put an example of the end I’m trying to ride.
– Matheus Francisco
And how are these data structured in Python? Ask this in the question too.
– Woss
Matheus, you need to collaborate by giving the necessary information. Saying that the data is in a CSV is useless without the data itself. Put exactly how the data is and already put the Python code you are using to read this data. Be objective and clear in the question. Information is trivial to solve the problem, do not omit it.
– Woss
there is not much code, there is only one line indicating that I am opening the file csv log_df = pd.read_csv('logs/logs.csv',delimiter=","), the data is exactly like this
– Matheus Francisco