Getting heatmap count based on pandas dataframe

432
June 23, 2017, at 4:23 PM

I have the foll. dataframe:

   ps_variable_1 ps_variable_2
0        Top 33%    Middle 33%
1     Bottom 33%    Middle 33%
2     Middle 33%    Middle 33%
3     Bottom 33%       Top 33%
4     Middle 33%    Bottom 33%
5        Top 33%    Bottom 33%
6        Top 33%    Bottom 33%
7     Bottom 33%    Middle 33%
8     Bottom 33%       Top 33%

I want to get a new dataframe out of it with 3 columns: Top 33% Middle 33% Bottom 33%. Each column has 3 rows as below:

               Top 33%     Middle 33%   Bottom 33%
Top 33%         
Middle 33%          
Bottom 33%          

For each grid cell, I want to count the number of occurrences based on the dataframe above. E.g. the first row contains the number of cases where ps_variable_1 is in the top 33% or top third-percentile, and the respective number of cases where ps_variable_2 is in the top third, middle third and bottom third percentile respectively. How do I do that, I would prefer a solution which can be extended to other ratios as well e.g. top 20%....

Answer 1

Option 1
pd.crosstab

pd.crosstab(df.ps_variable_1, df.ps_variable_2)

option 2
groupby and size

df.groupby(['ps_variable_1', 'ps_variable_2']).size().unstack(fill_value=0)

Option 3
np.bincount

u1, f1 = np.unique(df.ps_variable_1.values, return_inverse=True)
u2, f2 = np.unique(df.ps_variable_2.values, return_inverse=True)
n, m = u1.size, u2.size
b = np.bincount(f1 * m + f2, minlength=n * m).reshape(n, m)
pd.DataFrame(b, u1, u2)
READ ALSO
Trying to install mysqlclient with pip - Error Code 1

Trying to install mysqlclient with pip - Error Code 1

So I am trying to install the Python MySQL connectorI tried using the normal

374
Facing ImportError: DLL load failed while implementing Microsoft azure iothub_service_client

Facing ImportError: DLL load failed while implementing Microsoft azure iothub_service_client

I am trying to develop a python console application that implement IoT using Microsoft azureFor this purpose, I am following very basic tutorial made for python developers by Microsoft azure in my Windows 10 machine

402
My package on PyPi cannot find relevant files

My package on PyPi cannot find relevant files

I'm trying to upload my first package on PyPiEverything seems to be good except my function cannot access the relevant files within the package

236
Execute python3 from usr/local/bin out of php using shell_exec

Execute python3 from usr/local/bin out of php using shell_exec

I want to run a script with python3 version 36

305