Ranking Board - Python Data Science Course (Pandas) | Sololearn: Learn to code for FREE!

+2

Ranking Board - Python Data Science Course (Pandas)

Ranking Board You are given a DataFrame that includes the names and ranks of people. You need to take a rank as input and output the corresponding name column from the DataFrame as a Series. Note that the rank is an integer, so you need to convert the user input to an integer. Here is my code: ---------------------------------- import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'], 'rank': [4, 1, 3, 5, 2, 6] } rank = int(input()) df = pd.DataFrame(data, index=data['name']) y = df['name'] print(y.iloc[rank-1]) ------------------------------ the output I got is Bob but they want Bob Bob Name: name, dtype: object I tried my best, not sure how to get the output as the series that they want it. Please any clue would be great. Thank you so much. Please help! I love sololearn a lot

4/11/2021 8:08:50 PM

Alvin Nguyen

8 Answers

New Answer

+4

Alvin Nguyen I have explained the logic error in your code in great detail here and in the end, I have written my solution which is explained at every step. Please go through it, comment and uncomment codes to see everything that has been explained in action. This will clear all your doubts. https://code.sololearn.com/cA17a0a11A22/?ref=app

+1

I think I understand, something like this? https://code.sololearn.com/cYMBl3jZ3lE6/?ref=app

+1

thank you so much Steven M CHANDAN ROY I will go through my code and reach back on what I did wrong

+1

I recommend you look at @CHANDAN ROY 's code, it really helped me understand it!

+1

Try this: import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'], 'rank': [4, 1, 3, 5, 2, 6] } df = pd.DataFrame(data) userInputRank = int(input()) print(df[df['rank'] == userInputRank]['name'])

0

the rank number is not supposed to be print out tho :( thats why I just try to print just the name in the pandas

0

this code works: import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'], 'rank': [4, 1, 3, 5, 2, 6] } df = pd.DataFrame(data, index=data['name']) InputRank = int(input()) print(df[df['rank'] == InputRank]['name'])

0

import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'], 'rank': [4, 1, 3, 5, 2, 6] } df = pd.DataFrame(data, index=data['name']) print(df['name'].iloc[int(input())])