Write a function named "min_column" to find the column where the value of a given row is minimum. It takes two arguments, a Pandas dataframe and the index of the row. It should return the column that contains the minimum value of the row.
时间: 2024-01-19 12:02:33 浏览: 107
简单介绍Python中用于求最小值的min()方法
Sure, here's the code for the "min_column" function:
``` python
import pandas as pd
def min_column(df, row_index):
row = df.iloc[row_index] # get the row by index
min_val = row.min() # find the minimum value in the row
min_col = row.idxmin() # find the column index of the minimum value
return min_col
```
This function first takes a Pandas dataframe "df" and the index of the row "row_index" as arguments. It then selects the row with the given index using the "iloc" method. The minimum value of the row is found using the "min" method, and the column index of the minimum value is found using the "idxmin" method. Finally, the function returns the column index that contains the minimum value of the row.
阅读全文