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?
6 Respostas
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
+ 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/
+ 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])
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...
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()])
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])