+ 3

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

11th Apr 2021, 8:08 PM
Alvin Nguyen
Alvin Nguyen - avatar
11 Answers
+ 5
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
12th Apr 2021, 3:56 AM
CHANDAN ROY
CHANDAN ROY - avatar
+ 4
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']) u=int(input()) print(df["name"][df["rank"]==u])
7th Jul 2022, 11:35 AM
Monika Raut
Monika Raut - avatar
+ 2
thank you so much Steven M CHANDAN ROY I will go through my code and reach back on what I did wrong
12th Apr 2021, 4:49 AM
Alvin Nguyen
Alvin Nguyen - avatar
+ 1
I think I understand, something like this? https://code.sololearn.com/cYMBl3jZ3lE6/?ref=app
12th Apr 2021, 3:28 AM
Steven M
Steven M - avatar
+ 1
I recommend you look at @CHANDAN ROY 's code, it really helped me understand it!
26th Apr 2021, 6:54 PM
Mr._V1ct0r
Mr._V1ct0r - avatar
+ 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'])
5th Oct 2021, 12:54 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
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
12th Apr 2021, 3:48 AM
Alvin Nguyen
Alvin Nguyen - avatar
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'])
16th Feb 2022, 12:55 PM
Richard Janecz
Richard Janecz - avatar
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())])
4th Mar 2022, 1:37 PM
Atajan Orazmammedov
Atajan Orazmammedov - avatar
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']) rankInput=int(input()) selectedDataSet=df[(df['rank']==rankInput)] print(selectedDataSet['name'])
17th Nov 2022, 10:24 PM
Benjamin Fadina
Benjamin Fadina - avatar
0
I was trying to find a way to sort the the Data frame and use iloc function but sorting doesn't work for some reason
29th Dec 2022, 7:16 AM
Heider Diab
Heider Diab - avatar