Reinaldo Chaves
Reinaldo Chaves

Reputation: 995

In pandas how to create a dataframe from a list of dictionaries?

In python3 and pandas I have a list of dictionaries in this format:

a = [{'texto27/2': 'SENADO: PLS 00143/2016, de autoria de Telmário Mota, fala sobre maternidade e sofreu alterações em sua tramitação. Tramitação: Comissão de Assuntos Sociais. Situação: PRONTA PARA A PAUTA NA COMISSÃO. http://legis.senado.leg.br/sdleg-getter/documento?dm=2914881'}, {'texto27/3': 'SENADO: PEC 00176/2019, de autoria de Randolfe Rodrigues, fala sobre maternidade e sofreu alterações em sua tramitação. Tramitação: Comissão de Constituição, Justiça e Cidadania. Situação: PRONTA PARA A PAUTA NA COMISSÃO. http://legis.senado.leg.br/sdleg-getter/documento?dm=8027142'}, {'texto6/4': 'SENADO: PL 05643/2019, de autoria de Câmara dos Deputados, fala sobre violência sexual e sofreu alterações em sua tramitação. Tramitação: Comissão de Direitos Humanos e Legislação Participativa. Situação: MATÉRIA COM A RELATORIA. http://legis.senado.leg.br/sdleg-getter/documento?dm=8015569'}]

I tried to transform it into a dataframe with these commands:

import pandas as pd
df_lista_sentencas = pd.DataFrame(a)
df_lista_sentencas.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
texto27/2    1 non-null object
texto27/3    1 non-null object
texto6/4     1 non-null object
dtypes: object(3)
memory usage: 100.0+ bytes

But the generated dataframe has blank lines:

df_lista_sentencas.reset_index()
    index   texto27/2                                           texto27/3                                           texto6/4
0   0       SENADO: PLS 00143/2016, de autoria de Telmário...   NaN                                                 NaN
1   1       NaN                                                 SENADO: PEC 00176/2019, de autoria de Randolfe...   NaN
2   2       NaN                                                 NaN                                                 SENADO: PL 05643/2019, de autoria de Câmara do...

I would like to generate something like this:

texto27/2                                         texto27/3                                        texto6/4
SENADO: PLS 00143/2016, de autoria de Telmário... SENADO: PEC 00176/2019, de autoria de Randolfe.. SENADO: PL 05643/2019, de autoria de Câmara do...

Please, does anyone know how I can create a dataframe without blank lines?

Upvotes: 1

Views: 53

Answers (1)

YOLO
YOLO

Reputation: 21749

May be using bfill:

df = df_lista_sentencas.bfill().iloc[[0]]

print(df)

Upvotes: 1

Related Questions