How to select a column in pandas based on a name?

enter image description hereI'm trying to select a column.

So I work with the iris dataset. I want to only have the setosa flower for now so I stored it in a variable called Setosa.

Setosa = iris_df[iris_df['Name'] == 'Iris-setosa']

Then I used the df function because I wanted a specific column. Setosa = df['SepalLength']

Now I made a trace, I want to create a scatterplot. X-axis for sepallength and Y-axis for sepalwidth. But I don't know how to specify that I want it to be on the Setosa flower only?

trace1 = {
    "x": `df['SepalLength']`, 
    "y": `df['SepalWidth']`, 

I first tried "x": Setosa = df['SepalLength'] and "y": Setosa = df['SepalWidth'] but that doesn't work.

1 answer

  • answered 2019-06-11 23:32 Datanovice

    Just adding this as an answer since it worked

    iris_df['SepalWidth'].loc[iris_df['Name'] == 'Iris-setosa']

    Short explanation,

    you wanted to return a series where it matched a certain condition, to do this you need to first select the column with either bracket or dot notation and use a .loc or another filter to match your condition.

    best of luck