import numpy as np import pandas as pd actions = ['a', 'b', 'c', 'd'] a = [['2', '1.2', '4.2'], ['0', '10', '0.3'], ['1', '5', '0']] data = {'row1': [1, 2, 3, 4], 'row2': [5, 6, 7, 8], "row3": [9, 10, 11, 12]} # s = pd.DataFrame(data, columns=actions) s = pd.DataFrame(data) s.index = actions print(s) state_action = s.loc["a",:] print(state_action) print("++++"*20) print(state_action[state_action < np.max(state_action)].index) # 可以通过条件选择横坐标的范围Index(['row1', 'row2'], dtype='object') print(np.random.choice(state_action[state_action < np.max(state_action)].index)) # row2 通过随机选择获取 print(state_action[state_action == np.max(state_action)].index) # 可以通过条件选择横坐标的范围Index(['row3'], dtype='object') print(np.random.choice(state_action[state_action == np.max(state_action)].index)) # row3 通过随机选择获取
打印结果:
row1 row2 row3 a 1 5 9 b 2 6 10 c 3 7 11 d 4 8 12 row1 1 row2 5 row3 9 Name: a, dtype: int64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Index(['row1', 'row2'], dtype='object') row2