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
print(df.xs('G1'))


Picture showing the output of xs function in pandas
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
print(df.xs(1,level='Num'))


Picture showing the output of xs function in pandas
Click to Enlarge


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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250