Frequency table - Python

Asked

Viewed 333 times

0

Hello, I’m a beginner in the programming area and I’m having difficulty making a frequency table.

The table is this:

import math
import statistics
import numpy as np
import scipy.stats
import pandas as pd

escolaridade = {
  'Grau de escolaridade' : ['Primário', 'Secundário', 'Superior'],
   'Urbano' : [5025, 3155, 1720], 
   'Rural' : [2580, 285, 20]}

df = pd.DataFrame(escolaridade)
df

I need to calculate:

Proportion and % of individuals and each level of education, beyond some proportions, but I could not even sum up the figures to develop the calculations.

I wonder if someone could help me?

1 answer

1

This is just one of the ideas you can use:

Copying the dataframe to the original to be intact:

df1 = df.copy()

Adding up the lines:

df1['Total'] = df1.sum(axis = 1)

Adding the columns together and creating a new row in the dataframe:

df1 = df1.append(df1.sum(axis = 0), ignore_index = True)

Setting 'Education Degree' as index:

df1.set_index('Grau de escolaridade', inplace = True)

Renaming the index of the new line:

 df1.rename(index = {'PrimárioSecundárioSuperior': 'Total'}, inplace = True)

Calculating the frequency:

df1['% Urbano'] =  round(df1['Urbano'] / df1['Total'],4) * 100
df1['% Rural'] = round(df1['Rural'] / df1['Total'],4) * 100

Exit:

                      Urbano    Rural   Total   % Urbano    % Rural
Grau de escolaridade                    
            Primário    5025    2580    7605       66.07    33.93
            Secundário  3155    285     3440       91.72    8.28
            Superior    1720    20      1740       98.85    1.15
               Total    9900    2885    12785      77.43    22.57

Browser other questions tagged

You are not signed in. Login or sign up in order to post.