-2

I am quite new to python and coding, so sorry in advance if I may not be so clear. I have a dataframe where the rows correspond to IDs (f.ied) and the columns to several values (ICD10 codes). I want to select the rows which contain specific ICD10 codes. However, I could not find the right way to do so...I tried with loc and set but no luck...any help, please?

The dataframe is like that:

each rows corresponds to f.ied (IDs). I want to know which f.ied have specific codes: I20, I21, I22, I23, I24, I25.

Dataframe

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
Elix
  • 1
  • Welcome! does this help? https://stackoverflow.com/q/44416069/6692898 – RichieV Aug 04 '20 at 16:01
  • Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – code11 Aug 04 '20 at 16:50

1 Answers1

0
df = pd.DataFrame({'feid': [2, 4, 8, 0],
                    'f42002': [2, 0, 0, 0],
                    'f42003': [10, 'I21', 1, 'J10']})

df = df.set_index('feid')
df

DataFrame

    f42002  f42003
feid        
2   2   10
4   0   I21
8   0   1
0   0   J10

Desired items

mylist = ['I21', 'J10']

for i in mylist:
  print(df[(df['f42002']==i) | (df['f42003']==i)].index.values)

Result:

[4]
[0]
AtanuCSE
  • 8,832
  • 14
  • 74
  • 112