pandas ile veriyi dönüştürme

Python ile ETL ve ELT

Jake Roach

Data Engineer

Bir boru hattında veri dönüştürme

Veri, sonraki kullanıcılar için değer sağlamak adına uygun biçimde dönüştürülmelidir

Bir veri dönüşüm iş akışı.

pandas, tablosal veriyi dönüştürmek için güçlü araçlar sunar

  • .loc[]
  • .to_datetime()
Python ile ETL ve ELT

.loc[] ile kayıtları filtreleme

.loc[], bir DataFrame'in her iki boyutunu da dönüştürmenizi sağlar

# Yalnızca sıfır olmayan kayıtları tut
cleaned = raw_stock_data.loc[raw_stock_data["open"] > 0, :]
# Fazla sütunları kaldır
cleaned = raw_stock_data.loc[:, ["timestamps", "open", "close"]]
# Tek adımda birleştir
cleaned = raw_stock_data.loc[raw_stock_data["open"] > 0, ["timestamps", "open", "close"]]

.iloc[], DataFrame'leri tamsayı indeksleme ile filtreler

cleaned = raw_stock_data.iloc[[0:50], [0, 1, 2]]
Python ile ETL ve ELT

Veri tiplerini değiştirme

Veri tipleri, sonraki kullanım senaryeleri için sıkça dönüştürülmelidir

  • .to_datetime()
# "timestamps" sütunu şu an şöyle görünüyor: "20230101085731"
# "timestamps" sütununu datetime tipine dönüştür
cleaned["timestamps"] = pd.to_datetime(cleaned["timestamps"], format="%Y%m%d%H%M%S")
Timestamp('2023-01-01 08:57:31')
# "timestamps" sütunu şu an şöyle görünüyor: 1681596000011
# "timestamps" sütununu datetime tipine dönüştür
cleaned["timestamps"] = pd.to_datetime(cleaned["timestamps"], unit="ms")
Timestamp('2023-04-15 22:00:00.011000')
Python ile ETL ve ELT

Dönüşümleri doğrulama

Veri dönüştürme risklidir:

  • Bilgi kaybı
  • Hatalı veri oluşturma
# Bir DataFrame'i incelemenin birkaç yolu
cleaned = raw_stock_data.loc[raw_stock_data["open"] > 0, ["timestamps", "open", "close"]]
print(cleaned.head())
# En küçük ve en büyük kayıtları döndür
print(cleaned.nsmallest(10, ["timestamps"]))
print(cleaned.nlargest(10, ["timestamps"]))
Python ile ETL ve ELT

Haydi pratik yapalım!

Python ile ETL ve ELT

Preparing Video For Download...