spacetyper spacetyper - 3 months ago 9
Python Question

Select columns from a DataFrame based on values in a row in pandas

Say I have the same dataframe from this question:

A0 A1 A2 B0 B1 B2 C0 C1
0 0.84 0.47 0.55 0.46 0.76 0.42 0.24 0.75
1 0.43 0.47 0.93 0.39 0.58 0.83 0.35 0.39
2 0.12 0.17 0.35 0.00 0.19 0.22 0.93 0.73
3 0.95 0.56 0.84 0.74 0.52 0.51 0.28 0.03
4 0.73 0.19 0.88 0.51 0.73 0.69 0.74 0.61
5 0.18 0.46 0.62 0.84 0.68 0.17 0.02 0.53
6 0.38 0.55 0.80 0.87 0.01 0.88 0.56 0.72


But instead of wanting to return the minimum value of each row (of only B0, B1, B2)

A0 A1 A2 B0 B1 B2 C0 C1 Minimum
0 0.84 0.47 0.55 0.46 0.76 0.42 0.24 0.75 0.42
1 0.43 0.47 0.93 0.39 0.58 0.83 0.35 0.39 0.39
2 0.12 0.17 0.35 0.00 0.19 0.22 0.93 0.73 0.00
3 0.95 0.56 0.84 0.74 0.52 0.51 0.28 0.03 0.51
4 0.73 0.19 0.88 0.51 0.73 0.69 0.74 0.61 0.51
5 0.18 0.46 0.62 0.84 0.68 0.17 0.02 0.53 0.17
6 0.38 0.55 0.80 0.87 0.01 0.88 0.56 0.72 0.01


I want to return the column name which contains the minimum value of each row (of only B0, B1, B2):

A0 A1 A2 B0 B1 B2 C0 C1 col_of_min
0 0.84 0.47 0.55 0.46 0.76 0.42 0.24 0.75 B2
1 0.43 0.47 0.93 0.39 0.58 0.83 0.35 0.39 B0
2 0.12 0.17 0.35 0.00 0.19 0.22 0.93 0.73 B0
3 0.95 0.56 0.84 0.74 0.52 0.51 0.28 0.03 B2
4 0.73 0.19 0.88 0.51 0.73 0.69 0.74 0.61 B0
5 0.18 0.46 0.62 0.84 0.68 0.17 0.02 0.53 B2
6 0.38 0.55 0.80 0.87 0.01 0.88 0.56 0.72 B1


What's the best way to do this?

Answer

you can use filter() in conjunction with idxmin() method:

In [40]: x
Out[40]:
     A0    A1    A2    B0    B1    B2    C0    C1
0  0.84  0.47  0.55  0.46  0.76  0.42  0.24  0.75
1  0.43  0.47  0.93  0.39  0.58  0.83  0.35  0.39
2  0.12  0.17  0.35  0.00  0.19  0.22  0.93  0.73
3  0.95  0.56  0.84  0.74  0.52  0.51  0.28  0.03
4  0.73  0.19  0.88  0.51  0.73  0.69  0.74  0.61
5  0.18  0.46  0.62  0.84  0.68  0.17  0.02  0.53
6  0.38  0.55  0.80  0.87  0.01  0.88  0.56  0.72

In [41]: x['col_of_min'] = x.filter(like='B').idxmin(axis=1)

In [42]: x
Out[42]:
     A0    A1    A2    B0    B1    B2    C0    C1 col_of_min
0  0.84  0.47  0.55  0.46  0.76  0.42  0.24  0.75         B2
1  0.43  0.47  0.93  0.39  0.58  0.83  0.35  0.39         B0
2  0.12  0.17  0.35  0.00  0.19  0.22  0.93  0.73         B0
3  0.95  0.56  0.84  0.74  0.52  0.51  0.28  0.03         B2
4  0.73  0.19  0.88  0.51  0.73  0.69  0.74  0.61         B0
5  0.18  0.46  0.62  0.84  0.68  0.17  0.02  0.53         B2
6  0.38  0.55  0.80  0.87  0.01  0.88  0.56  0.72         B1