In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('data/src/sample_multi.csv', index_col=[0, 1, 2])
print(df)
In [3]:
print(df.index)
In [4]:
print(df.loc['A0', 'val_1'])
In [5]:
print(df.loc['A0', :])
In [6]:
print(df.loc['A0'])
In [7]:
print(df.loc['A0':'A2', :])
In [8]:
print(df.loc[['A0', 'A2'], :])
In [9]:
print(df.loc[('A0', 'B1'), :])
In [10]:
print(df.loc[('A0', 'B1', 'C2'), :])
In [11]:
print(df.loc[(['A0', 'A1'], ['B0', 'B3']), :])
In [12]:
# print(df.loc[(:, 'B1'), :])
# SyntaxError: invalid syntax
In [13]:
# print(df.loc[('A1':'A3', 'B2'), :])
# SyntaxError: invalid syntax
In [14]:
print(df.loc[(slice(None), 'B1'), :])
In [15]:
print(df.loc[(slice('A1', 'A3'), 'B2'), :])
In [16]:
print(df.loc[(slice('A1', 'A3'), ['B0', 'B2'], 'C1'), :])
In [17]:
print(df.loc[pd.IndexSlice[:, 'B1'], :])
In [18]:
print(df.loc[pd.IndexSlice['A1':'A3', 'B2'], :])
In [19]:
print(df.loc[pd.IndexSlice['A1':'A3', ['B0', 'B2'], 'C1'], :])
In [20]:
print(df.xs('B1', level='level_2'))
In [21]:
print(df.xs('C1', level=2))
In [22]:
print(df.xs(['B1', 'C2'], level=['level_2', 'level_3']))
In [23]:
print(df.xs(pd.IndexSlice['A1':'A3'], level='level_1'))
In [24]:
print(df.xs(slice('A1', 'A3'), level='level_1'))
In [25]:
# print(df.xs(['B1', 'B2'], level='level_2'))
# KeyError: ('B1', 'B2')
In [26]:
print(df.loc[pd.IndexSlice[:, ['B1', 'B2']], :])
In [27]:
df.loc[(['A0', 'A1'], ['B0', 'B3']), :] = -100
In [28]:
print(df)
In [29]:
df.loc[(['A0', 'A1'], ['B0', 'B3']), :] = [-200, -300]
In [30]:
print(df)
In [31]:
df.loc[(['A0', 'A1'], ['B0', 'B3']), :] = [[-1, -2], [-3, -4], [-5, -6], [-7, -8]]
In [32]:
print(df)
In [33]:
# df.xs(['B1', 'C2'], level=['level_2', 'level_3']) = 0
# SyntaxError: can't assign to function call