I want to print out C and C1 along with other row names that have 1 in the braf column. So I want my output to be C, C1,...
'Braf'
'C' 1
'NC' 0
'C1' 1
... ...
CodePudding user response:
If you want the indices of rows with a specific value in a specific column you can use groupby
:
df = pd.DataFrame({'BRAF': [0,1,0,1,1]})
d = df.index.groupby(df['BRAF'].eq(1))
# you can also directly use 0/1 if binary
groupA = d[True]
# [0, 2]
groupB = d[False]
# [1, 3, 4]
Or directly as dictionary:
out = df.index.groupby(np.where(df['BRAF'].eq(1), 'groupA', 'groupB'))
Output:
{'groupA': [1, 3, 4],
'groupB': [0, 2]}
CodePudding user response:
If need index values if match column Braf
use:
m = df['Braf'] == 1
groupA, groupbB = df.index[m].tolist(), df.index[~m].tolist()
If need get values of column 0
:
m = df['Braf'] == 1
groupA, groupbB = df.loc[m, 0].tolist(), df.loc[~m, 0].tolist()
If need extract values by first column:
m = (df['Braf'] == 1).to_numpy()
groupA, groupbB = df.iloc[m, 0].tolist(), df.iloc[~m, 0].tolist()
EDIT:
df = pd.DataFrame({'col':['a','b','c'],
'Braf':[1,0, 1]}, index=['C','NC','C1'])
print (df)
col Braf
C a 1
NC b 0
C1 c 1
#labels of indices
print (df.index)
Index(['C', 'NC', 'C1'], dtype='object')
#first column - selected by position - labels are same
print (df.iloc[:, 0])
C a
NC b
C1 c
Name: col, dtype: object
#column Braf - labels are same
print (df['Braf'])
C 1
NC 0
C1 1
Name: Braf, dtype: int64