在 pandas 中,如何计算逗号之间的项目,并按列类型划分? [英] In pandas, how to count items between commas, dividing between column types?
问题描述
在python3和pandas中,我具有以下数据框:
In python3 and pandas I have this dataframe:
df_selecao_atual.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 340 entries, 2 to 678
Data columns (total 26 columns):
cpf 339 non-null object
nome 340 non-null object
nome_completo 340 non-null object
partido 339 non-null object
uf 339 non-null object
cargo_parlamentar 340 non-null object
tipo 340 non-null object
classe 340 non-null object
numero 340 non-null object
único 340 non-null object
assunto 340 non-null object
data_inicial 340 non-null object
data_final 340 non-null object
andamento 340 non-null object
link 340 non-null object
transparencia 339 non-null object
conferencia 339 non-null object
data_conferencia 339 non-null object
resumo 332 non-null object
observacao 311 non-null object
link_noticia_tribunal_confiavel 104 non-null object
interessa 337 non-null object
ministro_relator 339 non-null object
processo_conectado 44 non-null object
situacao 337 non-null object
cadastro_push 337 non-null object
dtypes: object(26)
memory usage: 71.7+ KB
此数据框的每一行都包含有关法律程序的信息,每行一个诉讼案件
Each line of this dataframe has information about legal proceedings, one court case per line
"nome"列中的人名,例如:
The column "nome" has names of people, such as:
FULANO DE TAL
BELTRANO DA SILVA
SICRANO APARECIDO
NINGUEM AUGUSTO
"tipo"列中包含诉讼的类型,只有两种:
The "tipo" column has the types of lawsuits, only two types:
INQ
AP
"resumo"一栏对犯罪进行了调查,以便在法院诉讼中予以起诉.但是,每个法律程序都可能犯下一项或多项罪行.罪行之间用,"分隔:
And column "resumo" has crimes investigated for prosecution in court proceedings. But each legal process may 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
我需要数:
-
在每个名字中
In each name
按INQ和AP流程划分
Divided by INQ and AP processes
,"之间的每项犯罪行为的出现
The appearance of each individual crime between ","
以"resumo"列上方的示例为例:
Taking the example above the "resumo" column, something like this:
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
所有都是针对"John Doe"人的诉讼.前两行的类型为"AP",其余的为"INQ"
All are lawsuits against the person "John Doe". The first two lines are of type "AP" and the remaining "INQ"
那么约翰·道伊(John Doe)有:
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
我曾考虑过使用数据透视表,但是在每种类型的法院案件中,我都不得不只对犯罪类型进行一次计数.没用
I thought about using PivotTable, but I have the problem of having to count type of crime only once, in each type of court case. Did not work
df_selecao_atual.pivot_table(index=['nome', 'tipo', 'resumo'],aggfunc='size').reset_index()
请,有人知道我该怎么做吗?
Please, does anyone know how I could do it?
-/-
行示例如下
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'}}
推荐答案
使用示例数据,您可以按', '
进行汇总,然后使用Counter
Using your sample data, you can aggregate by ', '
and then use Counter
df2 = df.dropna().groupby(['nome', 'tipo']).resumo.agg(', '.join).str.split(', ').agg(Counter).reset_index()
nome tipo
CÉSAR MESSIAS AP {'Desvio de verbas públicas': 1}
INQ {'Desvio de verbas públicas': 1}
FLAVIANO MELO AP {'Peculato': 1, 'Gestão fraudulenta': 1, 'Desv...
INQ {'Prestação de contas rejeitada': 1}
Name: resumo, dtype: object
现在,以所需的方式格式化此信息变得更加容易.例如
Now it gets easier to format this information the way you want. For example,
df2 = df.dropna().groupby(['nome', 'tipo']).resumo.agg(', '.join).str.split(', ').agg(Counter).reset_index()
for _, g in df2.groupby('nome'):
x = f'{_} has: '
a = [f'{v} {t} for {k}' for d,t in zip(g.resumo, g.tipo) for (k, v) in d.items()]
lines = x + '\n'.join(a)
print(lines)
产生
CÉSAR MESSIAS has:
1 AP for Desvio de verbas públicas
1 INQ for Desvio de verbas públicas
FLAVIANO MELO has:
1 AP for Peculato
1 AP for Gestão fraudulenta
1 AP for Desvio de Verbas públicas
1 AP for Prestação de contas rejeitada
1 INQ for Prestação de contas rejeitada
这篇关于在 pandas 中,如何计算逗号之间的项目,并按列类型划分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!