Home > Back-end >  Print Out Rows Based on Condition
Print Out Rows Based on Condition

Time:07-12

I have the following code:

import pandas as pd

df = {'sport' : ['football', 'hockey', 'baseball', 'basketball'], 'league': ['NFL', 'NHL', 'MLB', 'NBA'], 'number': [1,2,3,4]}
df = pd.DataFrame(df)
df

if df['number'] >= 3:
    print(df['number'])

I am attempting to print out some rows of this data frame where the number column is greater or equal to 3. This should print out the last two rows in this particular example. What is wrong in my approach? I have tried if df['number'] >= 3: print(df['number']) as noted above.

Upon suggestion, the following code prints out the rows. How would I only return columns sport and number (instead of all columns)? enter image description here

CodePudding user response:

In your case do

print(df.loc[df['number'] >= 3,['sport', 'number']])

CodePudding user response:

You can do the following and it should print the rows with the matching values:

print(df[df['number'] >= 3])

or

print(df.loc[df['number'] >=3])

Saw your new comment. Here's how you would get the specific columns:

subset = df.loc[df['number'] >= 3]
print(subset[['sport', 'number']])

I used subset as another dataframe as it would hopefully be easier to manage and understand.

CodePudding user response:

IIUC, try,

df.loc[df['number'] >= 3, ['sport', 'number']]

Output:

        sport  number
2    baseball       3
3  basketball       4

A two-column dataframe where rows meet number condition. Using column filtering via a list of column headers.

  • Related