-2

I want to very simply call a column in a df based on a different columns value.

Below is what I would use, but how can I add another method on top of this that says give me all the values in a column called minutes or df['minutes'] if another called column_name is a specific value?

df.loc[df['column_name'] == some_value]

Sample Data:

column_name | Minutes
   1-5           19
   6-10          22
   11-15          8
   1-5           11
   6-10          33

I want to filter for column_name = any value in that column and return all values under the minutes column.

so if column_name is 1-5 return all values in Minutes column

Chris90
  • 1,868
  • 5
  • 20
  • 42

2 Answers2

2

A DataFrame usually contains multiple rows (and columns).

So if you ask whether particular column (say xx) has some value:

df.xx == 20

you will get a boolean Series with:

  • indices copied from df,
  • value stating whether xx column in this row == 20.

So I assume that you question about particular value in a given column should actually be expressed as: Does any element in this column have particular value?.

You can check it with any() function:

(df.xx == 22).any()

This time the result will be a single boolean.

In your case you can write:

if (df.column_name == '1-5').any():
    result = df.Minutes

Of course it is open to question what if not? Do you want another column in result variable?

Another approach is to set the column name in some variable, say src_col, based on some your logic.

Then, having this variable set, you can refer to the required column as:

result = df[src_col]

Note that this time:

  • the column name is between brackets,
  • but it is not surrounded with apostrophes,

so the target column name is expressed by the value of this variable.

And a remark about the comment by Chris90:

If you write df.loc[df['column_name'] == '1-5','Minutes'] you will get a single value from:

  • row containing 1-5 (string) in column_name,
  • Minutes column.

But you wrote that you wanted all values from this column.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • I am confused, when I run code: df.loc[df['column_name'] == '0-5','minutes'] This seems to give me all values from minutes column that have a value of 0-5 for column name column not a single value – Chris90 Jun 20 '19 at 05:32
  • The first expression between brackets (*df['column_name'] == '0-5'*) gives a *boolean* Series with *True* values if *column_name* has particular value. The result is that you get values from **all** such rows. Read about indexing in Pandas. – Valdi_Bo Jun 20 '19 at 05:56
1

Do you want the returned value to be something simpler than dataframe or series like:

[19,11]

this?

Then use tolist() method like this:

df.loc[df['column_name'] == "1-5"]["Minutes"].tolist()
Ruhshan
  • 121
  • 1
  • 8
  • I want the returned values to be something that can be plotted, was @user3508066 answer above not correct? ( df.loc[df['column_name'] == '1-5','Minutes )? – Chris90 Jun 20 '19 at 05:30
  • Yes that was also correct. Are you facing any issue when plotting? – Ruhshan Jun 20 '19 at 07:41