python - Selecting rows with similar index names in Pandas -


lets have pandas dataframe of following form:

      b  c a_1  1  4  2 a_2  3  3  5 a_3  4  7  2 b_1  2  9  8 b_2  7  2  6 b_3  5  4  1 c_1  3  1  3 c_2  8  6  6 c_3  9  3  7 

is there way select rows have similar names? in case of dataframe above mean selecting rows start a, or rows start b, etc.

using @akavall setup code

df = pd.dataframe(data = my_data, index=['a_1', 'a_2', 'b_1', 'b_2'], columns=['a', 'b'])  in [1]: my_data = np.arange(8).reshape(4,2)  in [2]: my_data[0,0] = 4  in [3]: df = pd.dataframe(data = my_data, index=['a_1', 'a_2', 'b_1', 'b_2'], columns=['a', 'b'])  in [5]: df.filter(regex='a',axis=0) out[5]:        b a_1  4  1 a_2  2  3  [2 rows x 2 columns] 

note in general better posed multi-index

in [6]: df.index = multiindex.from_product([['a','b'],[1,2]])  in [7]: df out[7]:        b 1  4  1   2  2  3 b 1  4  5   2  6  7  [4 rows x 2 columns]  in [8]: df.loc['a'] out[8]:      b 1  4  1 2  2  3  [2 rows x 2 columns]  in [9]: df.loc[['a']] out[9]:        b 1  4  1   2  2  3  [2 rows x 2 columns] 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -