Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet183/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   179   180   181   182   183   184   185   186   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Subsetting


When you need to filter out records but retain the information within the DataFrame you need to use a concept called subsetting. We’ll use the same condition as earlier, except this time we’ll use it to filter out records rather than create a true-false representation:

# using subsetting to filter out records and keep DataFrame intact df[ df["ages"] > 21 ]

Go ahead and run the cell. The output results in only those records whose ages are equal to or above the age of 21. We took the condition from above and wrapped it within brackets while accessing the df variable. Although it may look weird, the syntax representation is the following:
>>> dataframe_variable [ conditional statement to filter records with ]
You could also write the following for the same exact result:
>>> df[ can_drink ]
Remember that can_drink is a representation of true-false values, which means that the preceding statement will filter out all records that have the value of false.

Column Transformations


Rarely, if ever, will the columns in the original raw DataFrame imported from CSV or a database be the ones you need for your analysis. You will spend lots of time constantly transforming columns or groups of columns using general computational operations to produce new ones that are functions of the old ones. Pandas has full support for this and does it efficiently.

Generating a New Column with Data


To create a new column within a DataFrame, you use the same syntax as if you were adding a new key-value pair into a dictionary. Let’s create a column of fake data that represents how long the people within our DataFrame have been customers with our company:

1| # generating a new column of fake data for each record in the DataFrame to represent customer tenure
2| random.seed(321)
4| tenure = [ random.randint(0, 10) for x in range( len(df) )]
6| df["tenure"] = tenure # same as adding a new key-value pair in a dictionary
7| df.head( )

Go ahead and run the cell. The output will result in a new column created with random numbers for their tenure. We were able to add the column and its values on line 6. In Table 10-2, you’ll find the updated DataFrame, sorted by age.
Table 10-2. Adding a new column to the DataFrame





Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   179   180   181   182   183   184   185   186   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish