檢索每行的最大值列名稱
在由各種列和行組成的DataFrame 中,一個常見的任務是識別每行具有最大值的列。考慮以下 DataFrame:
通訊與搜尋業務一般生活方式<br>0 0.745763 0.050847 0.118644 0.084746<br>0 0.333333 0.00. 0.083333<br>0 0.617021 0.042553 0.297872 0.042553<br>0 0.435897 0.000000 0.410256 0.15384635338035 0.076923 0.410256 0.153846<br>
Communications and Search Business General Lifestyle Max<p>0 0.745763 0.050847 0.118644 0.084746 Communications <br>0 0.118644 0.084746 Communications <br>0 0.3033830303030303038 0.083333 商業 <br>0 0.617021 0.042553 0.297872 0.042553 通訊 <br>0 0.435897 0.000000 通訊 <br>0 0.435897 0.000000 通訊 <br>0 0.435897 0.000000 0.410256 . 0.076923 0.410256 0.153846 商業</p>
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Communications and Search': [0.745763, 0.333333, 0.617021, 0.435897, 0.358974], 'Business': [0.050847, 0.000000, 0.042553, 0.000000, 0.076923], 'General': [0.118644, 0.583333, 0.297872, 0.410256, 0.410256], 'Lifestyle': [0.084746, 0.083333, 0.042553, 0.153846, 0.153846] }) # Find the column index with the maximum value in each row max_column_idxs = df.idxmax(axis=1) # Create a new column with the column names df['Max'] = max_column_idxs # Display the updated DataFrame print(df)
以上是如何檢索 Pandas DataFrame 中每行具有最大值的列的名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!