153๐
โ
You can use value_counts
and to_dict
:
print df['status'].value_counts()
N 14
S 4
C 2
Name: status, dtype: int64
counts = df['status'].value_counts().to_dict()
print counts
{'S': 4, 'C': 2, 'N': 14}
๐คjezrael
14๐
An alternative one liner using underdog Counter
:
In [3]: from collections import Counter
In [4]: dict(Counter(df.status))
Out[4]: {'C': 2, 'N': 14, 'S': 4}
๐คColonel Beauvel
- [Django]-"Failed building wheel for psycopg2" โ MacOSX using virtualenv and pip
- [Django]-Aggregate() vs annotate() in Django
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-Django simple_tag and setting context variables
- [Django]-How do I force Django to ignore any caches and reload data?
- [Django]-Django CSRF Cookie Not Set
2๐
Can you convert df
into a list?
If so:
a = ['a', 'a', 'a', 'b', 'b', 'c']
c = dict()
for i in set(a):
c[i] = a.count(i)
Using a dict comprehension:
c = {i: a.count(i) for i in set(a)}
๐คChuck
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-Convert Django Model object to dict with all of the fields intact
- [Django]-Django: Redirect to previous page after login
1๐
See my response in this thread for a Pandas DataFrame output,
count the frequency that a value occurs in a dataframe column
For dictionary output, you can modify as follows:
def column_list_dict(x):
column_list_df = []
for col_name in x.columns:
y = col_name, len(x[col_name].unique())
column_list_df.append(y)
return dict(column_list_df)
๐คdjoguns
- [Django]-Allowing only super user login
- [Django]-Best practices for getting the most testing coverage with Django/Python?
- [Django]-Django/DRF โ 405 Method not allowed on DELETE operation
Source:stackexchange.com