How can I get the pandas value mode of the framework data?

I have pandas.DataFrameone containing numerous columns. I'm only interested in one of these columns ("names") whose type is "object". I want to answer three questions about this column:

  • What value (s) appears most often, excluding nan values?

  • How many values ​​match these criteria (number of values ​​in answer # 1)?

  • How often do these values ​​appear?

I started with a big data frame (df). The column that interests me is called "names." First, I used collection.Counter to get the number of entries for each unique value in the "names" column:

In [52]: cntr = collections.Counter([r for i, r in df['names'].dropna().iteritems()])
Out[52]: Counter({'Erk': 118,
    'James': 120,
    'John': 126,
    'Michael': 129,
    'Phil': 117,
    'Ryan': 126})

Then I converted the counter back to a data frame:

In [53]: df1 = pd.DataFrame.from_dict(cntr, orient='index').reset_index()
In [54]: df1 = df1.rename(columns={'index':'names', 0:'cnt'})

This gave me a pandas framework containing:

In [55]: print (type(df1), df1)
Out[55]: <class 'pandas.core.frame.DataFrame'>
       names    cnt
    0      Erk  118
    1    James  120
    2     Phil  117
    3     John  126
    4  Michael  122
    5     Ryan  126

. :

# 1 = [, ]

# 2 = 2

№ 3 = 126

, , , , dataframe dataframe.

+6
4

Counter, :

:

from collections import Counter

data = Counter({'Erk': 118, 'James': 120, 'John': 126,
                'Michael': 122, 'Phil': 117, 'Ryan': 126})

by_count = {}
for k, v in data.items():
     by_count.setdefault(v, []).append(k)
max_value = max(by_count.keys())
print(by_count[max_value], len(by_count[max_value]), max_value)

:

['John', 'Ryan'] 2 126
+4

, , : value_counts(). .

df1 = df['names'].value_counts()
# question 3
q3 = df1.max()
# question 1
q1 = df1.loc[df1 == q3].index.tolist()
# question 2
q2 = len(q1)
+1

from scipy import stats
Val,cnt=stats.mode(df1.cnt)
Val
Out[349]: array([126], dtype=int64)
cnt
Out[350]: array([2])

df1.names[df1.cnt.isin(Val)].tolist()
Out[358]: ['John', 'Ryan']
+1

pandas i.e

m = df1['cnt'].mode()
0    126
dtype: int64

sum(df1['cnt'].isin(m))
2

df1[df1['cnt'].isin(m)]['names']

3    Ryan
4    John
Name: names, dtype: object
0

All Articles