Computer scienceData scienceInstrumentsPandasData preprocessing with pandas

Searching within a pandas DataFrame

Penguin selection

Report a typo

You have a part of the Penguins dataset:

weighted_penguins.head()

Output:

+----+-----------+-----------+---------------+
|    | species   | island    |   body_mass_g |
|----+-----------+-----------+---------------|
|  0 | Adelie    | Torgersen |        4250.0 |
|  1 | Gentoo    | Biscoe    |        5200.0 |
|  2 | Chinstrap | Dream     |        4400.0 |
|  3 | Gentoo    | Biscoe    |        4550.0 |
|  4 | Chinstrap | Dream     |        3700.0 |
+----+-----------+-----------+---------------+

Replace the values in the dataset with thin penguin if a penguin weighs less than 3300 grams and with plump penguin if a penguin is heavier than 4000 grams. Store the result in weighted_penguins. Do not print the weighted_penguins dataframe.
Tip: You can do it by applying the .where() method twice, one after another.

Write a program in Python 3
import pandas as pd

penguins_dict = {'species': {0: 'Adelie', 1: 'Gentoo', 2: 'Chinstrap',
3: 'Gentoo', 4: 'Chinstrap', 5: 'Adelie',
6: 'Adelie', 7: 'Adelie'},
'island': {0: 'Torgersen', 1: 'Biscoe', 2: 'Dream',
3: 'Biscoe', 4: 'Dream', 5: 'Dream',
6: 'Torgersen', 7: 'Biscoe'},
'body_mass_g': {0: 4250.0, 1: 5200.0, 2: 4400.0, 3: 4550.0,
4: 3700.0, 5: 3550.0, 6: 3250.0, 7: 4275.0}}

weighted_penguins = pd.DataFrame(penguins_dict)


# your code here

weighted_penguins = ...
___

Create a free account to access the full topic