2017-04-21 17 views

答えて

4

value_counts種類の出力ので、あなたは、まず3インデックス値を選択するとvalue_countsを使用することができます。他の答えは、エラーを投げていた私として

print (df['Coefficient'].value_counts()) 
0.2 9 
0.5 9 
0.3 4 
0.1 4 
0.8 4 
0.6 3 
0.9 3 
0.4 2 
0.7 1 
Name: Coefficient, dtype: int64 

print (df['Coefficient'].value_counts().index[:3]) 
Float64Index([0.2, 0.5, 0.3], dtype='float64') 
+0

を使用して別の解決策は、あなたに感謝です!できます ! :) –

+0

うれしいことができます。幸運と素敵な日と週末! – jezrael

0

print(pd.Series(df['Coefficient']).value_counts())

。 もう1つ似た質問:How to find three most entered value in a column?

+2

Hmmm、私の設定は 'df = pd.DataFrame()' です。df ['Coefficient'] = [0.1,0.2,0.1,0.5,0.2,0.3,0.2,0.6,0.9,0.8,0.5,0.3,0.5 、0.8,0.4,0.1,0.2,0.5,0.9,0.7,0.2,0.5,0.5,0.2,0.8,0.3,0.6,0.5,0.2,0.2,0.4,0.1,0.3,0.9,0.8,0.2,0.5,0.6 、0.5] ' ' print(df) ' – jezrael

1

@jezraelと@Saikatによって提案された解決策は簡潔かつエレガントです。ここで

defaultdict:

from collections import defaultdict 

df = dict() 
df['Coefficient'] = [0.1,0.2,0.1,0.5,0.2,0.3,0.2,0.6,0.9,0.8,0.5,0.3,0.5,0.8,0.4,0.1,0.2,0.5,0.9,0.7,0.2,0.5,0.5,0.2,0.8,0.3,0.6,0.5,0.2,0.2,0.4,0.1,0.3,0.9,0.8,0.2,0.5,0.6,0.5] 

d = defaultdict(int) 

for i in df['Coefficient']: 
    d[i] += 1 

for w in sorted(d, key=d.get, reverse=True): 
     print(w, d[w]) 
0

df = pd.DataFrame({'Coefficient': [0.1,0.2,0.1,0.5,0.2,0.3,0.2,0.6,0.9,0.8,0.5,0.3,0.5,0.8,0.4,0.1,0.2, \ 
 
            0.5,0.9,0.7,0.2,0.5,0.5,0.2,0.8,0.3,0.6,0.5,0.2,0.2,0.4,0.1,0.3,0.9, \ 
 
            0.8,0.2,0.5,0.6,0.5]}) 
 
# Count the occurences of values 
 
print (df['Coefficient'].value_counts()) 
 
0.2 9 
 
0.5 9 
 
0.3 4 
 
0.1 4 
 
0.8 4 
 
0.6 3 
 
0.9 3 
 
0.4 2 
 
0.7 1 
 
Name: Coefficient, dtype: int64 
 

 
# Retain the top 3 most common 
 
print (df['Coefficient'].value_counts().iloc[:3]) 
 
0.2 9 
 
0.5 9 
 
0.3 4 
 
Name: Coefficient, dtype: int64 
 

 
# Only the values of the three most common in an array 
 
print (df['Coefficient'].value_counts().iloc[:3].index.values) 
 
[ 0.2 0.5 0.3]

関連する問題