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



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

ages

names

tenure

6

18

rebecca

4

5

20

tyler

6

2

22

sandy

2

0

25

Jess

5

3

29

ted

8

4

33

Barney

7

1

35

Jordan

5

apply( )


Adding new columns based on current data is known as “feature engineering.” It makes up a good portion of a data analysts’ job. Often, you won’t be able to answer the questions you have from the data you collect. Instead, you need to create your own data that is useful to answering questions. For this example, let’s try to answer the following question: “What age group does each customer belong to?”. You could look at the persons’ age and assume their age group; however, we want to make it easier than that. In order to answer this question easily, we’ll need to feature engineer a new column that represents each customer’s age group. We can do this by using the apply method on the DataFrame. The apply method takes in each record, applies the function passed, and sets the value returned as the new column data. Let’s check it out:

# feature engineering a new column from known data using a UDF def ageGroup(age):
return "Teenager" if age < 21 else "Adult" df["age_group"] = df["ages"].apply(ageGroup) df.head(10)

CHapter 10 INtroduCtIoN to data aNalYsIs
Go ahead and run the cell. Using the apply method, we’re able to create a new column that easily answers our question. When adding the new age_group column, we applied the ageGroup function based on the values in the ages column. It then iterated over each record in the DataFrame and set the return value of either “Teenager” or “Adult” as the value for the new age_group column. The apply method makes it easy for us to add new data with our own UDF. Take a look at Table 10-3.
Table 10-3. Feature engineering an age_group column





Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   180   181   182   183   184   185   186   187   ...   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