An warware: tace duk ginshiƙai a pandas

A cikin duniyar nazarin bayanai, sarrafa manyan bayanan bayanai na iya zama aiki mai ban tsoro. Ɗaya daga cikin mahimman sassan wannan tsari shine tace bayanai don samun bayanan da suka dace. Idan ya zo ga Python, ɗakin karatu mai ƙarfi pandas ya zo taimakonmu. A cikin wannan labarin, za mu tattauna yadda ake tace duk ginshiƙai a cikin pandas DataFrame. Za mu bi ta hanyar bayanin mataki-mataki na lambar kuma mu ba da zurfin fahimtar ɗakunan karatu da ayyuka waɗanda za a iya amfani da su don matsaloli iri ɗaya.

Gabatar da pandas

babban ɗakin karatu ne mai buɗewa wanda ke ba da tsarin bayanai masu sauƙin amfani da kayan aikin tantance bayanai don yaren shirye-shiryen Python. Yana taka muhimmiyar rawa a cikin yanayin ilimin kimiyyar bayanai kuma ya zama kayan aiki dole ne ga kowane masanin kimiyyar bayanai ko manazarci da ke aiki tare da Python. Daga cikin fasalulluka, pandas suna ba da tsarin bayanan farko guda biyu: DataFrame da kuma series. A DataFrame tebur ne mai girma biyu tare da maƙallan gatari ( layuka da ginshiƙai), yayin da jeri tsararru ce mai lamba ɗaya mai girma.

Don wannan labarin, za mu mai da hankali kan tace takamaiman ƙimar da ke cikin kowane ginshiƙi na pandas DataFrame. Don yin wannan, za mu yi amfani da pandas .isin() aiki tare da boolean masking.

Tace DataFrame

Don tace DataFrame a pandas, bi waɗannan matakan:

1. Shigo da ɗakin karatu na pandas
2. Ƙirƙiri DataFrame ko loda shi daga fayil
3. Ƙayyade ƙimar da kuke son tacewa
4. Aiwatar da tace ta amfani da aikin `.isin()` da abin rufe fuska na boolean
5. Nuna matattarar DataFrame

Bari mu nutse cikin lambar don fahimtar yadda yake aiki.

import pandas as pd

# Creating a DataFrame
data = {'Column1': [1, 2, 3, 4, 5],
        'Column2': [10, 20, 30, 40, 50],
        'Column3': ['A', 'B', 'A', 'B', 'A']}
df = pd.DataFrame(data)

# Define the values to filter
filter_values = [1, 3, 5, 'A']

# Apply the filter using .isin() and boolean masking
filtered_df = df[df.isin(filter_values).any(axis=1)]

# Display the filtered DataFrame
print(filtered_df)

A cikin wannan misalin, mun fara shigo da ɗakin karatu na pandas kuma mun ƙirƙiri DataFrame tare da ginshiƙai uku. Muna ayyana ƙimar da muke son tacewa (1, 3, 5, da 'A') sannan muyi amfani da tacewa ta amfani da aikin `.isin()' haɗe da abin rufe fuska na boolean. Aikin `kowane(axis=1)` yana duba idan kowace kimar da ke cikin jere ta cika ka'idojin tacewa. A ƙarshe, muna buga DataFrame da aka tace.

Aiki na .isin() da boolean masking

The .isin() Aiki a cikin pandas kayan aiki ne mai ma'ana don tace bayanai bisa jeri ko saitin dabi'u. Yana dawo da Boolean DataFrame mai siffar iri ɗaya da na asali, yana nuna waɗanne abubuwa ne ke cikin jeri ko saitin da aka bayar. A cikin yanayinmu, mun wuce jerin ƙimar da muke son tacewa.

Bolean masking wata dabara ce da ake amfani da ita a cikin pandas don tace bayanai cikin hikima. Ya ƙunshi yin amfani da abin rufe fuska na boolean (tsari na ƙimar Gaskiya da Ƙarya) zuwa tsarin bayanai don tace abubuwansa. A cikin mahallin matsalarmu, muna amfani da abin rufe fuska na boolean tare da aikin .isin() don dawo da layuka masu ɗauke da ƙimar da ake so.

Tare da cikakkiyar fahimtar ɗakin karatu na pandas, Tsarin DataFrame, da aikin .isin(), za mu iya tace kowane pandas DataFrame yadda ya kamata. Waɗannan fasahohin suna ba mu damar bincika manyan bayanan bayanai da kuma fitar da bayanai masu mahimmanci cikin sauƙi, yin pandas zuwa ɗakin karatu don nazarin bayanai a Python.

Shafi posts:

Leave a Comment