Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> import numpy as np
- >>> import pandas as pd
- >>> pd.__version__
- u'0.23.4'
- >>> index = pd.MultiIndex(levels=[[u'bar', u'baz', u'foo', u'qux'],
- [u'one', u'two', u'three', u'four']],
- labels=[[0, 0, 1, 1, 2, 2, 3, 3],
- [0, 0, 1, 1, 2, 2, 3, 3]],
- names=[u'first', u'second'])
- >>> df = pd.DataFrame(np.random.randn(8, 4), index=index)
- >>> df.columns = ['c0', 'c1', 'c2', 'c3']
- >>> df
- c0 c1 c2 c3
- first second
- bar one -2.366973 -0.887149 -0.301309 1.312207
- one 1.266500 0.864888 -1.407567 0.265077
- baz two -1.926091 -0.671274 -0.295846 0.679759
- two -0.212970 0.136552 0.219074 0.541827
- foo three -0.698288 -2.059952 0.248811 0.947879
- three -2.017481 0.163013 -0.906551 -0.102474
- qux four -1.083530 0.097077 0.224977 0.251739
- four 0.943804 1.356789 -0.953357 0.592986
- >>> two_data = df[df.index.get_level_values('second') == 'two']
- >>> mask = (two_data['c1'] > 0)
- >>> mask = mask.values
- array([False, True])
- >>> df[df.index.get_level_values('second') == 'two'][mask].drop('two', level=1)
- Empty DataFrame
- Columns: [c0, c1, c2, c3]
- Index: []
- >>> df[df.index.get_level_values('second') == 'two'].iloc[mask].drop('two', level=1)
- Empty DataFrame
- Columns: [c0, c1, c2, c3]
- Index: []
- >>> df
- c0 c1 c2 c3
- first second
- bar one -2.366973 -0.887149 -0.301309 1.312207
- one 1.266500 0.864888 -1.407567 0.265077
- baz two -1.926091 -0.671274 -0.295846 0.679759
- two -0.212970 0.136552 0.219074 0.541827
- foo three -0.698288 -2.059952 0.248811 0.947879
- three -2.017481 0.163013 -0.906551 -0.102474
- qux four -1.083530 0.097077 0.224977 0.251739
- four 0.943804 1.356789 -0.953357 0.592986
- >>> df[df.index.get_level_values('second') == 'two'][mask].drop('two', level=1, inplace=True)
- >>> df
- c0 c1 c2 c3
- first second
- bar one -2.366973 -0.887149 -0.301309 1.312207
- one 1.266500 0.864888 -1.407567 0.265077
- baz two -1.926091 -0.671274 -0.295846 0.679759
- two -0.212970 0.136552 0.219074 0.541827
- foo three -0.698288 -2.059952 0.248811 0.947879
- three -2.017481 0.163013 -0.906551 -0.102474
- qux four -1.083530 0.097077 0.224977 0.251739
- four 0.943804 1.356789 -0.953357 0.592986
- >>> df[df.index.get_level_values('second') == 'two'].iloc[mask].drop('two', level=1, inplace=True)
- >>> df
- c0 c1 c2 c3
- first second
- bar one -2.366973 -0.887149 -0.301309 1.312207
- one 1.266500 0.864888 -1.407567 0.265077
- baz two -1.926091 -0.671274 -0.295846 0.679759
- two -0.212970 0.136552 0.219074 0.541827
- foo three -0.698288 -2.059952 0.248811 0.947879
- three -2.017481 0.163013 -0.906551 -0.102474
- qux four -1.083530 0.097077 0.224977 0.251739
- four 0.943804 1.356789 -0.953357 0.592986
- c0 c1 c2 c3
- first second
- bar one -2.366973 -0.887149 -0.301309 1.312207
- one 1.266500 0.864888 -1.407567 0.265077
- baz two -1.926091 -0.671274 -0.295846 0.679759
- foo three -0.698288 -2.059952 0.248811 0.947879
- three -2.017481 0.163013 -0.906551 -0.102474
- qux four -1.083530 0.097077 0.224977 0.251739
- four 0.943804 1.356789 -0.953357 0.592986
Add Comment
Please, Sign In to add comment