An warware: pandas ya maye gurbin ƙimar shafi

Pandas babban ɗakin karatu ne na Python mai ƙarfi da ake amfani da shi don sarrafa bayanai da bincike. Ɗayan aiki na gama gari da ake yi tare da bayanai shine maye gurbin ƙimar ginshiƙi bisa wasu ƙa'idodi, kamar daidaitawa ko taswira zuwa wasu ƙima. A cikin wannan labarin, za mu bincika yadda ake amfani da wannan aikin yadda ya kamata ta amfani da ɗakin karatu na Pandas. Ko kai masanin kimiyar bayanai ne, mai tsara shirye-shirye, ko ƙwararren masani da ke zurfafa zurfafa cikin duniyar abubuwan da ke tattare da salon salon bayanai, wannan ilimin zai yi matukar amfani.

Makullin fahimtar wannan aiki ya ta'allaka ne wajen ƙware ginannun ayyukan da ɗakin karatu na Pandas ke bayarwa. Musamman, za mu mai da hankali kan amfani da `masanya()`, `taswira()`, da `aika ()» ayyuka don sarrafa ƙimar ginshiƙi bisa ma'auni daban-daban.

import pandas as pd

# Sample data
data = {'Fashion_Style': ['Boho', 'Grunge', 'Preppy', 'Vintage', 'Athleisure'],
        'Colors': ['Earthy tones', 'Dark shades', 'Bright tones', 'Muted tones', 'Monochrome']}

df = pd.DataFrame(data)

# Replacing column values
df['Colors'] = df['Colors'].replace(['Earthy tones', 'Monochrome'], ['Warm tones', 'Contrast tones'])

print(df)

Bayanin mataki-mataki na Code

1. Da farko, muna shigo da ɗakin karatu na Pandas azaman 'pd'. Wannan al'ada ce ta gama gari, kuma tana ba mu damar kiran ayyukan Pandas tare da gajeriyar 'pd'.
2. Bayan haka, mun ƙirƙiri ƙamus mai suna `data` mai ɗauke da ginshiƙan 'Fashion_Style' da 'Launuka', da kuma ƙimar su.
3. Sa'an nan kuma mu ƙirƙiri DataFrame mai suna `df' ta amfani da aikin `pd.DataFrame()` tare da 'data' dictionary a matsayin hujja.
4. Bayan haka, muna amfani da aikin `maye gurbin()' don maye gurbin takamaiman dabi'u a cikin rukunin 'Launuka'. A cikin misalinmu, muna maye gurbin ' sautunan Duniya' da 'Sautunan Dumi' da 'Monochrome' da 'Sautunan Sabanin'.
5. A ƙarshe, muna buga sabunta DataFrame `df' don duba sakamakon.

Pandas Gina Ayyukan Ayyuka don Maye gurbin Ƙimar Rumbun

Pandas yana ba da ayyukan ginanni da yawa don aiki tare da ƙimar shafi a cikin DataFrames. Daga cikin waɗannan, mun gano `maye gurbin()`, `taswira()`, da `taba()` da amfani musamman idan ya zo ga maye gurbin kimar ginshiƙi bisa sharuɗɗa daban-daban.

maye gurbin (): Ana amfani da wannan aikin don maye gurbin ƙayyadaddun ƙididdiga a cikin DataFrame ko Series. Ana iya amfani da shi zuwa wani ginshiƙi ko gabaɗayan DataFrame, kuma yana goyan bayan maganganun yau da kullun don daidaita tsarin ci gaba.

df['Colors'] = df['Colors'].replace(['Earthy tones', 'Monochrome'], ['Warm tones', 'Contrast tones'])

taswira (): Aikin `taswira()` yayi kama da `maye gurbin()`, amma yana aiki da wani aiki da aka bayar ko ƙamus ga kowane kashi a cikin jeri. Wannan na iya zama da amfani lokacin da kuke buƙatar taswirar ƙimar ginshiƙi zuwa sabbin ƙima bisa ƙayyadaddun ƙayyadaddun ƙa'idodi.

color_mapping = {'Earthy tones': 'Warm tones', 'Monochrome': 'Contrast tones'}
df['Colors'] = df['Colors'].map(color_mapping)

tambaya (): Aikin 'apply()' kayan aiki ne mai ƙarfi wanda ke aiwatar da aikin da aka bayar tare da axis na DataFrame. Ana iya amfani da shi akan gabaɗayan DataFrame ko takamaiman ginshiƙai don cimma ɗimbin sauye-sauye.

def update_colors(color_value):
    if color_value == 'Earthy tones':
        return 'Warm tones'
    elif color_value == 'Monochrome':
        return 'Contrast tones'
    else:
        return color_value
        
df['Colors'] = df['Colors'].apply(update_colors)

Tare da waɗannan ayyuka a hannunku, yanzu kun shirya don magance ayyuka daban-daban na sarrafa bayanai a cikin Pandas, kamar maye gurbin ƙimar ginshiƙi a cikin DataFrames. Wannan ilimin ba wai kawai yana aiki a fagen kimiyyar bayanai da shirye-shirye ba har ma yana tabbatar da amfani yayin nazarin salon salon zamani, gano abubuwan da ke tasowa, da fahimtar mahimmancin tarihi na salo da launuka daban-daban.

Shafi posts:

Leave a Comment