'Question about selecting rows and columns from a DataFrame (Python) [duplicate]

I'm following this tutorial to select specific rows and columns from a DataFrame.

The tutorial example shows that you can use: adult_names = titanic.loc[titanic["Age"] > 35, "Name"]

to obtain:

1 Cumings, Mrs. John Bradley (Florence Briggs Th...

6 McCarthy, Mr. Timothy J

11 Bonnell, Miss. Elizabeth

13 Andersson, Mr. Anders Johan

15 Hewlett, Mrs. (Mary D Kingcome)

Name: Name, dtype: object

However, if I want to access Miss. Elizabeth Bonnell, I'd have to use adult_names[11] (even though she's the 3rd name older than 35).

Is there a way to populate an array with these values so that the first name would be in adult_names[0], the second name would be in adult_names[1], the third name would be in adult_names[2], etc.?



Solution 1:[1]

do you want something like this ?

adult_names = list(titanic.loc[titanic["Age"] > 35, "Name"].values)

Solution 2:[2]

you could also just use adults_name and use the iloc. adult_name[11] is the same as adult_name.iloc[2]. iloc is indexing by position.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DataSciRookie
Solution 2 Rabinzel