Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercício ETL pandas de músicas mais ouvidas #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions exercicios/para-casa/ETL_pandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pandas as pd

df = pd.read_csv("../../material/mais_ouvidas_2024.csv")

print(df.head())
print(df.info())

for column in df.columns:
if df[column].dtype == "object":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

essa condição induz ao erro de duas maneiras:

1 - Você esta convertendo todas as colunas tipo object sem destinção, inclusive as que DEVEM ser object. e 2 coisas podem acontecer : ou vai falhar em converter e retornar um erro ou por alguma razão vai converter em um valor inesperado , o que vai desqualificar seus dados
3 - Está convertendo TODOS os dados para float, até intendo que essa seja a intenção mas analisar se os dados devem ser mesmo float e modificar o tipo conforme a necessidade tb deve ser algo para se pensar a respeito

df[column] = df[column].str.replace(",", "").astype(float, errors='ignore')

df["Release Date"] = pd.to_datetime(df["Release Date"])
print(df.dtypes)

df["Streaming Popularity"] = df[["Spotify Popularity", "YouTube Views", "TikTok Likes", "Shazam Counts"]].mean(axis=1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como uma boa pratica, salve essa lista de colunas em uma variável que indique o significado dos valores assim o seu código se torna mais legível para quem está trabalhando nele


print(df["Streaming Popularity"])

df["Total Streams"] = df[["Spotify Streams", "YouTube Views", "TikTok Views", "Pandora Streams", "Soundcloud Streams"]].sum(axis=1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O mesmo para a lista usada aqui no sum


print(df["Total Streams"])

filtered_df = df[(df["Spotify Popularity"] > 80) & (df["Total Streams"] > 1_000_000)]

print(filtered_df.head())

filtered_df.to_json("./filtered_list.json", index=False)
1 change: 1 addition & 0 deletions exercicios/para-casa/filtered_list.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions exercicios/para-sala/ETL_pandas_by_me.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pandas as pd

# ['TransactionID', 'Date', 'MobileModel', 'Brand', 'Price', 'UnitsSold', 'TotalRevenue', 'CustomerAge', 'CustomerGender', 'Location', 'PaymentMethod']

df = pd.read_csv("../../material/mobile_sales.csv")

# Mostrar as 10 primeiras linhas ao invés do padrão 5
# print(df.head(n=10))

# print(df.head())
# print(df.columns)
df_valores_nulos = df.isnull()
# print(df_valores_nulos.sum())
# Verificar dados duplicados
#print(df.duplicated().sum())
df.drop_duplicates()

df["Date"] = pd.to_datetime(df["Date"], format="mixed")
#print(df.dtypes)
#print(df["Date"])

df["Total Sales Value"] = df["Price"] * df["UnitsSold"]
# print(df.columns)
# print(df["Total Sales Value"])

profit_per_product = 0.30
df["Profit Margin"] = (df["Price"] * profit_per_product ) * df["UnitsSold"]

filtered_df = df[(df["Total Sales Value"] > 100_000) & (df["Profit Margin"] > 20_000)]

print(filtered_df.head())

filtered_df.to_csv("./filtered_list.csv", index=False)