0

Pandas "Locating contacts"

Locating Contacts You are given a dictionary that contains names and numbers of people. You need to create a DataFrame from the dictionary and add an index to it, which should be the name values. Then take a name from user input and output the row in the DataFrame, which corresponds to that row. my code below: import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom'], 'number': ['1234', '5678', '2222', '1111', '0909'] } df = pd.DataFrame(data) # print(df) name = df.loc[input()] print(name) I'm certain I got the last line wrong. Can I get some help on how to handle input in this one?

10th Jun 2021, 1:42 AM
William Weidner
William Weidner - avatar
6 Réponses
0
You didn't set your index in the df: df=pd.DataFrame(data, index=data['name']) it should work after that. Also, I did it slightly different... https://code.sololearn.com/cYMBl3jZ3lE6/?ref=app
10th Jun 2021, 4:23 AM
Steven M
Steven M - avatar
+ 5
Hi William, Here is codes by Giovanni - https://code.sololearn.com/c6IIvHrY3X9c/?ref=app please see this how it's running, helpful for you :) information of .loc - https://www.google.com/amp/s/www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-pandas-extracting-rows-using-loc/amp/
10th Jun 2021, 2:58 AM
Abhiyantā
Abhiyantā - avatar
+ 2
import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom'], 'number': ['1234', '5678', '2222', '1111', '0909'] } df=pd.DataFrame(data,index=data["name"]) searchName=input() print(df.loc[searchName])
17th Nov 2022, 9:49 PM
Benjamin Fadina
Benjamin Fadina - avatar
0
import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom'], 'number': ['1234', '5678', '2222', '1111', '0909'] } df = pd.DataFrame(data,index=['James', 'Billy', 'Bob', 'Amy', 'Tom']) name =input() print(df.loc[name]) It works...
7th Jul 2022, 11:12 AM
Monika Raut
Monika Raut - avatar
0
import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom'], 'number': ['1234', '5678', '2222', '1111', '0909'] } df = pd.DataFrame(data, index = data["name"]) print(df.loc[input()])
4th Dec 2022, 4:37 PM
Ronalds Skulme
Ronalds Skulme - avatar
0
import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom'], 'number': ['1234', '5678', '2222', '1111', '0909'] } dr = pd.DataFrame(data,index=['James', 'Billy', 'Bob', 'Amy', 'Tom']) print(dr) a = input() print(dr.loc[a])
29th Dec 2022, 10:43 AM
Pareshkumar Chaudhari