簡體   English   中英

如何在python pandas中獲得每列n個最頻繁或最高的值?

[英]How to get the n most frequent or top values per column in python pandas?

我的數據框看起來像:

df:
    A   B
0   a   g
1   f   g
2   a   g
3   a   d
4   h   d
5   f   a

對於每列前 2 個最頻繁的值 (n=2),輸出應為:

top_df:
    A   B
0   a   g
1   f   d

謝謝

這應該工作

n = 2
df.apply(lambda x: pd.Series(x.value_counts().index[:n]))

喜歡的東西, 可以幫助

maxes = dict()
for col in df.columns:
    frequencies = df[col].value_counts()
    # value counts automatically sorts, so just take the first 2
    max[col] = frequencies[:2]

解決方案:
要獲得n最頻繁的值,只需使用.value_counts()子集並獲取索引:

import pandas as pd

df = pd.read_csv('test.csv')

# METHOD 1 : Lil lengthy and inefficient
top_dict = {}
n_freq_items = 2
top_dict['A'] = df.A.value_counts()[:n_freq_items].index.tolist()
top_dict['B'] = df.B.value_counts()[:n_freq_items].index.tolist()
top_df = pd.DataFrame(top_dict)

print(top_df)
df.apply(lambda x: pd.Series(x.value_counts()[:n_freq_items].index))

# METHOD 2 : Small, and better : taking this method from @myccha. As I found this better
top_df = df.apply(lambda x: pd.Series(x.value_counts()[:n_freq_items].index))
print(top_df)

輸入數據:

# test.csv
A,B
a,g
f,g
a,g
a,d
h,d
f,a

輸出:

   A  B
0  a  g
1  f  d

注意:我從@myccha那里得到了解決方案,這是這篇文章的另一個答案,因為我發現他的答案更有幫助,將其添加為方法 2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM