1

I have this dataframe,

import pandas as pd

one = pd.DataFrame({
    'id':[1,2,3,4,5],
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub3','sub4','sub5'],
    'Marks_scored':[98,90,87,69,78],
    'Rank': [1,1,2,10,7]})

two = pd.DataFrame({
    'id':[1,2,3,4,5],
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub1','sub2','sub3','sub7','sub8'],
    'Marks_scored':[89,80,79,97,88],
    'Rank': [1,2,5,1,2]})

I have concatenated these two dataframes like this.

df = pd.concat([one,two],keys=['x','y'])

Now I want only the values from key 'x' only. How can I achieve that?

2

1 Answer 1

1

Use DataFrame.loc or DataFrame.xs:

print (df.loc['x'])
print (df.xs('x'))

   id    Name subject_id  Marks_scored  Rank
0   1    Alex       sub1            98     1
1   2     Amy       sub2            90     1
2   3   Allen       sub3            87     2
3   4   Alice       sub4            69    10
4   5  Ayoung       sub5            78     7
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.