0
How do I extract a column using Pandas?
Thia code fails at code coach: import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'], 'rank': [4, 1, 3, 5, 2, 6] } df = pd.DataFrame(data, index=[4, 1, 3, 5, 2, 6]) rank = int(input()) a = df.loc[rank] print(a["name"])
9 Antworten
+ 1
# this is how i would approach it:
import pandas as pd
data = {
'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'],
'rank': [4, 1, 3, 5, 2, 6]
}
df = pd.DataFrame(data, index=“rank”) # name of column to index. you can leave this blank.
rank = int(input()) # im assuming this is for the row.
a = df.loc[rank, :] # you need to have the row and column seperated by comma.
print(a["name"])
# i think this will work now.
+ 1
then maybe this:
print(df.loc[rank, “name”])
+ 1
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'])
rank = int(input())
a = df[(df["rank"]==rank)]["name"]
print(a)
+ 1
glad you were able to figure it out.
0
Thanks, I've tried this, it gives us the name but not in the format the coach expects. It wants (for instance) Bob Bob
Name name
dtype object
0
I habe to return the name column as a series
0
No it doesn't retunr Name and dtype
0
i think this is the closest i am able to get for dtype…
import pandas as pd
data = {
'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'],
'rank': [4, 1, 3, 5, 2, 6]
}
df = pd.DataFrame(data)
print(df.loc[:, "name"].dtypes)
0
Thanks