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'))
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'))
Click to Enlarge