An warware: yadda ake barin kwanakin pandas datetime

Fashion da shirye-shirye na iya zama kamar duniyoyi guda biyu mabanbanta, amma idan aka zo batun nazarin bayanai da hasashen yanayin, suna iya haduwa da kyau. A cikin wannan labarin, za mu bincika matsala gama gari don nazarin bayanai a cikin masana'antar keɓe: keɓance takamaiman kwanaki daga bayanan kwanan wata na pandas. Wannan na iya zama da amfani musamman lokacin nazarin ƙira, abubuwan da ke faruwa, da bayanan tallace-tallace. Za mu yi bayani mataki-mataki kan lambar, kuma mu tattauna ɗakunan karatu da ayyuka daban-daban waɗanda za su taimaka mana cimma burinmu.

Pandas da Kwanan wata a cikin Fashion

Pandas sanannen ɗakin karatu ne na Python wanda aka fi amfani dashi don bincike da sarrafa bayanai. A cikin duniyar salo, ana iya amfani da shi don tara bayanai masu yawa don gano abubuwan da ke faruwa, bincika abubuwan da abokin ciniki ke so, da kuma hasashen yanayin gaba. Pandas yana goyan bayan ayyukan kwanan wata, yana ba mu damar yin aiki tare da kwanan wata da lokuta ba tare da wahala ba.

A yawancin lokuta, ya zama dole a bar takamaiman ranaku ko jeri na kwanaki daga saitin bayanan mu. Misali, ƙila mu so mu keɓance ƙarshen mako ko hutu don mai da hankali kan mahimman kwanakin siyarwa, kamar Black Friday ko Cyber ​​​​Litinin.

Fahimtar Matsalar

Bari mu ce muna da bayanan da ke ɗauke da bayanan tallace-tallace na yau da kullun a tsarin CSV, kuma muna son yin nazarin bayanan yayin ban da ƙarshen mako. Don cimma wannan, za mu fara da shigo da bayanan bayanan ta amfani da pandas, sannan za mu sarrafa bayanan don cire karshen mako.

Ga tsarin mataki-mataki:

1. Shigo da dakunan karatu da ake bukata.
2. Load da dataset.
3. Maida ginshiƙin kwanan wata zuwa tsarin kwanan wata (idan ba a riga a cikin wannan tsarin ba).
4. Tace dataframe don ware karshen mako.
5. Yi nazarin bayanan da aka tace.

lura: Ana iya amfani da wannan hanyar zuwa kowane saitin bayanai inda aka adana kwanan wata a cikin wani shafi na daban.

# Step 1: Import the necessary libraries
import pandas as pd
from pandas.tseries.offsets import BDay

# Step 2: Load the dataset
data = pd.read_csv('sales_data.csv')

# Step 3: Convert the date column to datetime format
data['date'] = pd.to_datetime(data['date'])

# Step 4: Filter the dataframe to exclude weekends
filtered_data = data[data['date'].dt.dayofweek < 5]

# Step 5: Analyze the filtered data
print(filtered_data.head())

Tafsirin Code

A cikin toshe lambar da ke sama, za mu fara da shigo da mahimman ɗakunan karatu guda biyu: pandas da BDay (ranar kasuwanci) daga pandas.tseries.offsets. Muna loda saitin bayanai ta amfani da aikin pandas karanta_csv, kuma tabbatar da ginshiƙin kwanan wata yana cikin tsarin kwanan wata.

The dt.dayofweek sifa ta dawo ranar mako a matsayin lamba (Litinin: 0, Lahadi: 6). Don tace karshen mako, muna ajiye layuka ne kawai tare da ƙimar ranar mako-mako ƙasa da 5.

A ƙarshe, muna nazarin bayanan da aka tace ta hanyar buga layuka na farko ta amfani da kai() aiki.

Ƙarin Ayyuka da Dakunan karatu

Ana iya ƙara wannan hanyar don haɗawa da wasu sharuɗɗan tacewa ko don aiki tare da jeri daban-daban na kwanan wata. Wasu ɗakunan karatu masu amfani da ayyuka waɗanda zasu iya tallafawa wannan tsari sun haɗa da:

  • NumPy: Laburare don lissafin lambobi a cikin Python, wanda za'a iya amfani dashi don ingantaccen sarrafa tsararru da ayyukan lissafi.
  • Kwanan Wata: Module a daidaitaccen ɗakin karatu na Python wanda ke taimaka mana aiki tare da kwanan wata da lokuta cikin sauƙi.
  • kwanan wata: Aiki a cikin pandas wanda ke ba mu damar ƙirƙirar kewayon kwanakin bisa ga saitunan mitoci daban-daban, kamar kwanakin kasuwanci, makonni, ko watanni.

Ta hanyar yin amfani da waɗannan kayan aikin da dabaru tare da pandas da magudin kwanan wata, zaku iya ƙirƙirar ƙaƙƙarfan ayyukan bincike na bayanai waɗanda ke ba da takamaiman buƙatun masana'antar kayan kwalliya, kamar gano abubuwan da ke faruwa, zaɓin abokin ciniki, da ayyukan tallace-tallace.

Shafi posts:

Leave a Comment