import csv
import pandas as pd
from matplotlib import pyplot as plt

# Declaring workout data to df variable
df = pd.read_csv('workout.csv')

# Declaring name variable and storing name data into it
name = df['name']

# Calculating the mass moved
moved_mass_data = df['sets'] * df['reps'] * df['weight']

# storing mass moved data into the df data frame
df['mass moved'] = moved_mass_data

# turning moved mass data frame into a list
moved_mass = moved_mass_data.to_list()

# turning exercise name data frame into a list
exercise_name = name.to_list()

# zip names and mass moved together
exercise_moved_mass = zip(exercise_name, moved_mass)

# converting the zip into a list
exercise_moved_mass_list = list(exercise_moved_mass)

# exercise_moved_mass_list prints
[('Facepull', 480), ('Lat Pull Down', 600), ('Dumbbell Bench', 720), ('Dumbbell Bench', 1200), ('Dumbbell Bench', 2880), ('Dips', 3780), ('Incline Fly', 720), ('Incline Fly', 800), ('Incline Fly', 1200), ('Incline Fly', 800)]

#How could I make a function that would turn list to this
[('Facepull', 480), ('Lat Pull Down', 600), ('Dumbbell Bench', 4,800), ('Dips',3780), ('Incline Fly', 2800)]
1
John T 2 Май 2021 в 22:44

2 ответа

Лучший ответ

В ожидаемых выходных данных наклонный полет должен быть ('Наклонный полет', 3520), если да,

Этот код должен работать->

b = {}
for i in exercise_moved_mass_list:
  try:
    b[i[0]]+=i[1]
  except:
    b[i[0]]=i[1]

c = [(x, y) for x, y in b.items()]

Выход:

[('Facepull', 480),
 ('Lat Pull Down', 600),
 ('Dumbbell Bench', 4800),
 ('Dips', 3780),
 ('Incline Fly', 3520)]
0
KnowledgeGainer 2 Май 2021 в 20:09

Другой метод с использованием вашего фрейма данных df:

>>> df[["name", "mass moved"]].groupby("name").sum().to_records().tolist()

[('Dips', 3780),
 ('Dumbbell Bench', 4800),
 ('Facepull', 480),
 ('Incline Fly', 3520),
 ('Lat Pull Down', 600)]
0
Corralien 2 Май 2021 в 20:16