Issue
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)?
Solution
In your case do
print(df.loc[df['number'] >= 3,['sport', 'number']])
Answered By - BENY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.