How do I filter so that row 2 is == '1' and find the min value in row 1. And it prints the column its in?
So in this case the result would only bring up Driver 3 [.941, 0.210, 1] because row 2 is 1 and row1 is smaller than the rest of that would also have '1' in row 2.
When I try np.amin(df, axis=2) I get the min values i want but row 2 returns a 0 instead of 1.
CodePudding user response:
You first need to select the subset of columns you're interested in, then find from those the minimum value, then figure out which column its in (or both at once with argmin
). (Note that pandas
API somewhat prefers filtering out rows vs columns.)
First setup some example data
import pandas as pd
import numpy as np
data = np.arange(24).reshape(4,6)
data[1,3:5]=1 # Adjust some values in row 2
df = pd.DataFrame(data, columns=['A', 'B', 'C', 'D', 'E', 'F'])
So transpose columns and rows
dfT = df.T
Get the columns where row 2 ==1
dfFiltered = dfT[dfT[1] == 1]
Find the minimum row with the minimum value and get its index
dfFiltered.index[dfFiltered[0].argmin()]
This produces 'D'
for this example.
CodePudding user response:
First of all, for this kind of application you really want your DataFrame to be transposed. It is just not convenient to filter on columns.
import pandas as pd
A = pd.DataFrame(...)
B = A.transpose()
# Now you can filter the drivers where "column" 2 is equal to 1:
C = B[B[2] == 1]
# And find where the minimum of "column" 1 is:
idx = C[1].argmin()
# Finally, find out the driver and its information:
driver = C.index[idx]
info = C.iloc[idx]
# info2 = C.loc[driver] # alternative
# To get the format you want:
print(driver, info.values)