An warware: ƙara sabon shafi zuwa pandas dataframe

A cikin wannan labarin, za mu bincika tsarin ƙara sabon shafi zuwa Pandas DataFrame, sanannen ɗakin karatu a Python don sarrafa bayanai da bincike. Za mu tattauna maganin wannan matsala, mu bi ta hanyar bayani mataki-mataki na lambar, da kuma rufe wasu batutuwa da ayyuka masu alaƙa a cikin ɗakin karatu na Pandas. Pandas ɗakin karatu ne da aka yi amfani da shi sosai wanda ke nuna babban tsarin bayanai da kayan aiki, cikakke don ingantaccen bincike da sarrafa ayyuka.

Da farko, bari mu ɗauka muna da tsarin bayanai a cikin hanyar Pandas DataFrame kuma muna son ƙara sabon shafi a ciki. Wannan buƙatu ce ta gama gari a matakin shirya bayanai, galibi ana buƙata don aikin injiniyan fasali ko don samar da ƙarin bayani dangane da ginshiƙan da ke akwai. Bari mu nutse cikin yadda za a iya cimma hakan.

Ƙara sabon shafi zuwa Pandas DataFrame

Za mu fara da shigo da ɗakin karatu da ake buƙata da ƙirƙirar samfurin DataFrame.

import pandas as pd

data = {'Name': ['Alex', 'Tom', 'Nick', 'Sam'],
        'Age': [25, 28, 23, 22],
        'City': ['NY', 'LA', 'SF', 'Chicago']}

df = pd.DataFrame(data)

Yanzu, bari mu ƙara sabon shafi 'Ƙasa' zuwa DataFrame ɗin mu tare da ƙimar tsoho, a ce 'Amurka'.

df['Country'] = 'USA'

Wannan layin code mai sauƙi zai ƙara sabon shafi mai suna 'Ƙasa' zuwa DataFrame' df' ɗinmu na yanzu tare da darajar 'Amurka' a duk layukansa. DataFrame da aka sabunta zai yi kama da haka:

  Name  Age     City Country
0  Alex   25      NY     USA
1   Tom   28      LA     USA
2  Nick   23      SF     USA
3   Sam   22  Chicago     USA

Bayanin lambar mataki-mataki

Bari mu rushe lambar kuma mu fahimce shi mataki-mataki.

1. Da farko, muna shigo da ɗakin karatu na Pandas ta amfani da daidaitaccen laƙabi 'pd'. Wannan yana ba mu damar samun damar ayyukan Pandas da azuzuwan ta amfani da prefix 'pd'.

import pandas as pd

2. Na gaba, za mu ƙirƙiri ƙamus 'data' mai ɗauke da wasu bayanan samfurin. Kowane maɓalli a cikin ƙamus yana wakiltar sunan shafi, kuma ƙimar da ta dace ita ce lissafin ƙimar wannan shafi.

data = {'Name': ['Alex', 'Tom', 'Nick', 'Sam'],
        'Age': [25, 28, 23, 22],
        'City': ['NY', 'LA', 'SF', 'Chicago']}

3. Sa'an nan kuma mu mayar da wannan ƙamus zuwa Pandas DataFrame abu ta amfani da `pd.DataFrame()` aikin.

df = pd.DataFrame(data)

4. A ƙarshe, don ƙara sabon shafi, kawai muna amfani da ma'aikacin ɗawainiya "=" tare da DataFrame, samar da sabon sunan shafi a cikin maƙallan murabba'i da ƙididdige ƙimar tsoho. A cikin yanayinmu, mun ƙara ginshiƙin 'Ƙasa' tare da tsohuwar ƙimar 'Amurka'.

df['Country'] = 'USA'

Laburaren Pandas da ayyuka masu alaƙa

Pandas babban ɗakin karatu ne na Python, musamman dacewa don sarrafa bayanai, tsaftacewa da ayyukan bincike. Yana bayar da manyan bayanai guda biyu: DataFrame da kuma series. A DataFrame tsarin bayanai ne mai girma biyu tare da gatura masu lakabi ( layuka da ginshiƙai). A Series, a gefe guda, tsararru ce mai lamba ɗaya mai girma ɗaya mai iya riƙe bayanai kowane iri.

Wasu ayyukan Pandas gama gari masu alaƙa da ƙara, gyarawa da share ginshiƙai a cikin DataFrame sune kamar haka:

  • saka(): Don saka ginshiƙi a ƙayyadadden matsayi.
  • sauke(): Don cire shafi daga DataFrame.
  • sake suna(): Don sake suna ginshiƙin DataFrame.
  • sanya(): Don ƙirƙirar sabon shafi dangane da sakamakon magana.

Don haka, ƙara sabon shafi zuwa Pandas DataFrame yana da sauƙi kuma mai inganci. A cikin wannan labarin, mun rufe ainihin hanyar ƙara sabon shafi tare da ƙimar da ba ta dace ba kuma mun ba da cikakkun bayanai game da matakan da abin ya shafa. Mun kuma gabatar da Pandas a matsayin ɗakin karatu mai ƙarfi na sarrafa bayanai kuma mun tattauna wasu ayyuka masu alaƙa don sarrafa ginshiƙan DataFrame. Ta hanyar ƙware waɗannan fasahohin, za ku kasance da wadatattun kayan aiki don gudanar da ayyuka da yawa na sarrafa bayanai a Python.

Shafi posts:

Leave a Comment