import pandas as pd
import numpy as np
nrows = 2
ncols = 6
df = pd.DataFrame(np.random.rand(nrows,ncols), index=list(range(nrows)), columns=['col1', 'col2', 'col3', 'col4', 'col5', 'col6'])
print(df)
for row in range(nrows):
maxCol1, maxCol2, maxCol3 = 0, 0, 0
for col in range(ncols):
if(df.iloc[row, col] > df.iloc[row, maxCol1]):
maxCol3 = maxCol2
maxCol2 = maxCol1
maxCol1 = col
elif(df.iloc[row, col] > df.iloc[row, maxCol2]):
maxCol3 = maxCol2
maxCol2 = col
elif(df.iloc[row, col] > df.iloc[row, maxCol3]):
maxCol3 = col
print("{} {} {} {}".format(row+1, df.columns[maxCol1], df.columns[maxCol2], df.columns[maxCol3]))
會產生類似的結果:
col1 col2 col3 col4 col5 col6
0 0.116408 0.316982 0.811001 0.964968 0.019989 0.674887
1 0.241690 0.429653 0.456779 0.398780 0.861042 0.149223
1 col4 col3 col6
2 col5 col3 col2