Pandas ɗakin karatu ne mai ƙarfi kuma mai sassauƙa a cikin Python, wanda aka saba amfani dashi don sarrafa bayanai da ayyukan bincike. Ɗaya daga cikin mahimman abubuwan da ke cikin Pandas shine series abu, wanda ya ƙunshi nau'i-nau'i ɗaya, mai lakabi. A cikin wannan labarin, za mu mai da hankali kan takamaiman matsala: ƙara kalma zuwa kowane abu a cikin jerin Pandas. Za mu yi tafiya ta hanyar mafita, muna tattauna lambar mataki-mataki don fahimtar ayyukanta na ciki. Bugu da ƙari, za mu tattauna dakunan karatu masu alaƙa, ayyuka, da kuma ba da haske game da matsaloli iri ɗaya.
Ayyukan da ke hannunsu shine ɗaukar Tsarin Pandas wanda ya ƙunshi igiyoyi, kuma ƙara kalma zuwa kowane abu a cikin tsararru. Maganin da muka gabatar anan zai yi amfani da Pandas da ginanniyar damarsa don magance wannan matsala cikin inganci da inganci.
Da farko, bari mu shigo da laburaren da ake bukata ta hanyar shigo da Pandas da fara bayanan da ke cikin Silsilar.
import pandas as pd data = ['item1', 'item2', 'item3'] series = pd.Series(data)
Na gaba, muna buƙatar ayyana kalmar da muke son ƙarawa. A cikin wannan misalin, za mu yi amfani da kalmar “misali” azaman kalmar don haɗawa kowane abu a cikin Jerin Pandas.
word_to_add = "example"
Yanzu za mu ci gaba ta hanyar amfani da .tambaya() hanyar da za a ƙara kalmar da ake so zuwa kowane kashi a cikin Jerin.
series_with_added_word = series.apply(lambda x: x + ' ' + word_to_add) print(series_with_added_word)
Wannan zai haifar da fitarwa mai zuwa:
0 item1 example 1 item2 example 2 item3 example dtype: object
Yanzu da muka sami nasarar cimma burin, bari mu tattauna lambar da abubuwan da ke tattare da shi dalla-dalla.
Pandas Series
A Pandas Series Tsari ne mai girma ɗaya, mai lakabin mai iya riƙe kowane nau'in bayanai, gami da ints, masu iyo, da sauran abubuwa. Akwai hanyoyi da yawa don ƙirƙirar Tsarin Pandas, kamar yadda aka nuna a matakin farkon mu. A Series yana kula da alamun fihirisa, don haka yana ba da izini don ingantaccen amfani da sarrafa bayanai.
Ayyukan Lambda da amfani () Hanyar
A aikin lambda aiki ne na layi, wanda ba a san shi ba a Python. Yana da amfani a lokuttan da ayyana aiki na yau da kullun na iya zama mai wahala ko maras buƙata. Waɗannan ayyuka na iya samun kowane adadin gardama amma magana ɗaya kawai, wanda ake kimantawa kuma ana dawowa. Musamman a yanayin hanyar .apply(), ayyukan lambda suna sauƙaƙe lambar.
The .tambaya() Hanyar, a gefe guda, tana sauƙaƙe amfani da aiki zuwa kowane abu a cikin Pandas Series ko DataFrame. Yana haɓaka da kyau ta kowane nau'i, yana ba da damar haɓaka kewayon keɓancewa yayin sarrafa bayanai.
A cikin maganinmu, mun yi amfani da aikin lambda tare da hanyar .apply() don cimma sakamakon da ake so. Ta hanyar amfani da wannan dabarar, mun rage adadin lambar da ake buƙata kuma mun sami nasarar ƙara kalma zuwa kowane abu a cikin Tsarin Pandas.
A ƙarshe, mun nuna bambancin Pandas, musamman ta hanyar Pandas Series, don magance matsalar sarrafa bayanai na gama gari. Ta hanyar amfani da hanyar .apply() da ayyukan lambda, mun ƙetare da kyau kuma mun canza abubuwan da ke cikin Jerin. Wannan ya zama babban misali na yadda za a iya magance irin waɗannan batutuwa da kuma shawo kan su ta amfani da kayan aiki mai ƙarfi wato Pandas.