Articles → Pandas → xs function in pandas

xs function in pandas






Purpose





Example


import numpy as np
import pandas as pd

# Index Levels
outside = ['G1','G1','G1','G2','G2','G2']
inside = [1,2,3,1,2,3]
hier_index = list(zip(outside,inside))
hier_index = pd.MultiIndex.from_tuples(hier_index)

df = pd.DataFrame(np.random.randn(6,2),index=hier_index,columns=['A','B'])

#naming multi level index
df.index.names = ['Group','Num']

# xs sample
df.xs(['G1',1])



Click to Enlarge



import numpy as np
import pandas as pd

# Index Levels
outside = ['G1','G1','G1','G2','G2','G2']
inside = [1,2,3,1,2,3]
hier_index = list(zip(outside,inside))
hier_index = pd.MultiIndex.from_tuples(hier_index)

df = pd.DataFrame(np.random.randn(6,2),index=hier_index,columns=['A','B'])

#naming multi level index
df.index.names = ['Group','Num']

# xs another sample
df.xs(1,level='Num')



Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, April 26, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250