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"])

28th Aug 2021, 4:54 PM
Thiago Silva
Thiago Silva - avatar
9 Respuestas
+ 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.
28th Aug 2021, 5:04 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 1
then maybe this: print(df.loc[rank, “name”])
28th Aug 2021, 5:35 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 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)
28th Aug 2021, 10:39 PM
Thiago Silva
Thiago Silva - avatar
+ 1
glad you were able to figure it out.
28th Aug 2021, 11:01 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
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
28th Aug 2021, 5:13 PM
Thiago Silva
Thiago Silva - avatar
0
I habe to return the name column as a series
28th Aug 2021, 5:21 PM
Thiago Silva
Thiago Silva - avatar
0
No it doesn't retunr Name and dtype
28th Aug 2021, 5:38 PM
Thiago Silva
Thiago Silva - avatar
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)
28th Aug 2021, 5:56 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
0
Thanks
28th Aug 2021, 11:13 PM
Thiago Silva
Thiago Silva - avatar