Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Python Pandas value_counts in SQL?

Tags:

python

sql

pandas

I learned python and pandas before SQL, so this question is a bit basic.

For example, I have a type column with values like 1, 2, 3.

Then when I do df['type'].value_counts, I can get the statistics of the type, maybe something like

1: 1000 rows
2: 220 rows
3: 100 rows

I want to know What is the equivalent in SQL? I believe it should be something about group_by and count?

like image 592
cqcn1991 Avatar asked Mar 14 '18 02:03

cqcn1991


People also ask

Can you use pandas in SQL?

Pandasql is a python library that allows manipulation of a Pandas Dataframe using SQL. Under the hood, Pandasql creates an SQLite table from the Pandas Dataframe of interest and allow users to query from the SQLite table using SQL.

What is value_counts () in pandas?

Return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occurring element. Excludes NA values by default.

Is pandas an alternative to SQL?

Both Pandas and SQL are essential tools for data scientists and analysts. There are, of course, alternatives for both but they are the predominant ones in the field. Since both Pandas and SQL operate on tabular data, similar operations or queries can be done using both.

Which of the following merge methods of pandas can be used as SQL?

Pandas DataFrame merge() Method.


2 Answers

If you want to know how many times each value occurs in a column, use:

SELECT type, count(*)
FROM table
GROUP BY type
like image 89
Turophile Avatar answered Oct 28 '22 16:10

Turophile


SELECT type, count(1) as num_types
FROM table
GROUP BY type

will return the equivalent row counts.

like image 25
Ash Chakraborty Avatar answered Oct 28 '22 18:10

Ash Chakraborty