python中pd如何取每列topk
python中pd如何取每列topk
-
核心函数.nlargest()
-
具体实现:
win_path = '/work/GDN/draw/win_result.csv' FFT_df = pd.read_csv(FFT_path) win_df_topk = {} for col in win_df.columns: # 获取当前列的Top-K值 top_k_values = win_df[col].nlargest(K) # 使用nsmallest获取最小值,如果想要最大值使用nlargest win_df_topk[col] = top_k_values.tolist() win_df_topk = pd.DataFrame(win_df_topk) print(win_df_topk)