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?

1 Answers1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252