In python3 and pandas I have a dataframe which contains for each line informations about legal proceedings.
The column "nome" has names of people, the "tipo" column has the types of lawsuits, only two types INQ
and AP
.
And column "resumo" has crimes investigated for prosecution in court proceedings. But each legal process may consist of one or more crimes. And the crimes are separated by ",":
Peculato, Lavagem de Dinheiro
Corrupção passiva, Ocultação de bens, Lavagem de dinheiro
Corrupção passiva, Lavagem de dinheiro, Crimes Eleitorais
Crimes Eleitorais, Lavagem de dinheiro
Peculato
Quadrilha ou Bando, Crimes da Lei de licitações, Peculato
I need to count:
For each name
Divided by INQ and AP processes
The appearance of each individual crime between ","
Taking the example above the "resumo" column, and assuming they all concern the person "John Doe".
The first two lines are of type AP
and the remaining INQ
, then John Doe has:
1 AP for Peculato
2 AP for Lavagem de dinheiro
1 AP for Corrupção passiva
1 AP for Ocultação de bens
1 INQ for Corrupção passiva
2 INQ for Lavagem de dinheiro
2 INQ for Crimes Eleitorais
2 INQ for Peculato
1 INQ for Quadrilha ou Bando
1 INQ for Crimes da Lei de licitações
A sample of the rows look like
df_selecao_atual[['tipo', 'resumo', 'nome']].head(5).to_dict()
{'tipo': {2: 'INQ', 3: 'AP', 4: 'INQ', 5: 'INQ', 6: 'AP'},
'resumo': {2: 'Desvio de verbas públicas',
3: 'Desvio de verbas públicas',
4: nan,
5: 'Prestação de contas rejeitada',
6: 'Peculato, Gestão fraudulenta'},
'nome': {2: 'CÉSAR MESSIAS',
3: 'CÉSAR MESSIAS',
4: 'FLAVIANO MELO',
5: 'FLAVIANO MELO',
6: 'FLAVIANO MELO'}}
On this database I already had an answer that worked very well in this link: In pandas, how to count items between commas, dividing between column types?
But now I need to not only show on the screen, but create a dataframe. Like this:
nome tipo resumo count
Fulano de tal INQ Peculato 4
Fulano de tal INQ Ocultação de Bens 1
Fulano de tal INQ Corrupção ativa 2
Fulano de tal INQ Investigação Penal 3
Fulano de tal AP Peculato 1
Fulano de tal AP Corrupção passiva 2
Beltrano da Silva INQ Peculato 2
Beltrano da Silva INQ Lavagem de dinheiro 5
Beltrano da Silva AP Lavagem de dinheiro 1
Please, does anyone know how I could create this dataframe?