Reputation: 995
In Python3 and pandas I have a one column dataframe. On each line there is a list, with dictionaries inside. It was obtained from a file:
import pandas as pd
geral = pd.read_csv("mandados_12_abr_2018_RJ.csv",sep=';',encoding = 'latin_1')
geral.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5298 entries, 0 to 5297
Data columns (total 4 columns):
mandados 5298 non-null object
mensagem 0 non-null float64
paginador 5298 non-null object
sucesso 5298 non-null bool
dtypes: bool(1), float64(1), object(2)
memory usage: 129.4+ KB
df1 = pd.DataFrame(geral['mandados'])
df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5298 entries, 0 to 5297
Data columns (total 1 columns):
mandados 5298 non-null object
dtypes: object(1)
memory usage: 41.5+ KB
Sample content, of column "mandados" - a list, with dictionaries inside:
[{'id': 409, 'numeroMandado': '2251-65.2012.8.19.0066.0001', 'nomeParte': 'CARLOS HENRIQUE BELARMINO DE OLIVEIRA', 'dataMandado': '2012-01-31', 'orgao': 'TJRJ', 'situacao': 'Aguardando Cumprimento', 'detalhes': ['Data de nascimento: 02/07/1987', 'Nacionalidade: Brasileira', 'Sexo: Masculino', 'Nome da Genitora: Luiza Maria Belarmino Dias', 'Nome do Genitor: Geraldo De Oliveira', 'Carteira de identidade: 213004088']}, {'id': 408, 'numeroMandado': '11451-97.2012.8.19.0001.0001', 'nomeParte': 'DANIEL NASCIMENTO SILVA', 'dataMandado': '2012-01-31', 'orgao': 'TJRJ', 'situacao': 'Aguardando Cumprimento', 'detalhes': ['Sexo: Masculino', 'Nacionalidade: Brasileira', 'Nome do Genitor: Não Declarado', 'Data de nascimento: 27/02/1993', 'Nome da Genitora: Marluce Nascimento Silva', 'Carteira de identidade: 276885555']}, {'id': 407, 'numeroMandado': '11451-97.2012.8.19.0001.0002', 'nomeParte': 'THIAGO SABINO DA SILVA', 'dataMandado': '2012-01-31', 'orgao': 'TJRJ', 'situacao': 'Aguardando Cumprimento', 'detalhes': ['Nome do Genitor: Joao Batista Benvindo Da Silva', 'Sexo: Masculino', 'Nome da Genitora: Eliza Sabino Miranda', 'Data de nascimento: 02/03/1988', 'Nacionalidade: Brasileira', 'Carteira de identidade: 216708446']}, ...
I want to create a new dataframe in which columns are the items of the dictionaries in each row of the "df1" dataframe. Selecting only these columns:
nomeParte, orgao, numeroMandado, dataMandado, situacao and detalhes
Is it possible to do that?
Upvotes: 0
Views: 71
Reputation: 10326
If you could post just the first few rows of your .csv, so that I could ensure I have the same data, that would be helpful. But this seems to do what you want:
list_dicts = [{'id': 409, 'numeroMandado': '2251-65.2012.8.19.0066.0001', 'nomeParte': 'CARLOS HENRIQUE BELARMINO DE OLIVEIRA', 'dataMandado': '2012-01-31', 'orgao': 'TJRJ', 'situacao': 'Aguardando Cumprimento', 'detalhes': ['Data de nascimento: 02/07/1987', 'Nacionalidade: Brasileira', 'Sexo: Masculino', 'Nome da Genitora: Luiza Maria Belarmino Dias', 'Nome do Genitor: Geraldo De Oliveira', 'Carteira de identidade: 213004088']}, {'id': 408, 'numeroMandado': '11451-97.2012.8.19.0001.0001', 'nomeParte': 'DANIEL NASCIMENTO SILVA', 'dataMandado': '2012-01-31', 'orgao': 'TJRJ', 'situacao': 'Aguardando Cumprimento', 'detalhes': ['Sexo: Masculino', 'Nacionalidade: Brasileira', 'Nome do Genitor: Não Declarado', 'Data de nascimento: 27/02/1993', 'Nome da Genitora: Marluce Nascimento Silva', 'Carteira de identidade: 276885555']}, {'id': 407, 'numeroMandado': '11451-97.2012.8.19.0001.0002', 'nomeParte': 'THIAGO SABINO DA SILVA', 'dataMandado': '2012-01-31', 'orgao': 'TJRJ', 'situacao': 'Aguardando Cumprimento', 'detalhes': ['Nome do Genitor: Joao Batista Benvindo Da Silva', 'Sexo: Masculino', 'Nome da Genitora: Eliza Sabino Miranda', 'Data de nascimento: 02/03/1988', 'Nacionalidade: Brasileira', 'Carteira de identidade: 216708446']}]
cols = ["nomeParte", "orgao", "numeroMandado", "dataMandado", "situacao", "detalhes"]
df = pd.DataFrame(list_dicts)[cols]
where I've just copy-pasted in the list of dictionaries from your post. You have this list as a column of another dataframe, correct? And that's what's giving you issues? If this list is the column 'mandados'
of geral
, I think you could do
pd.DataFrame(mandados.geral.tolist())[cols]
EDIT - after looking at the data in question
The format of this data file is quite unusual, but we can get what you want. Two main things:
mandados
column are just read as strings (they're not converted into lists or such automatically); this is normal and expected behavior. We'll have to do the conversion ourselves.mandados
is a list of dictionaries - I'm assuming that you want a dataframe made from the combination of all of those lists?Here's something that I think will do what you want: first we convert from strings to lists of dictionaries (using literal_eval
; this is a safe version of eval
; it will just convert strings that could be viewed as Python lists, tuples, dicts, and numeric types into those types, but it won't execute arbitrary code) then we combine all of the lists into one, and finally make a dataframe.
import pandas as pd
from ast import literal_eval
df = pd.read_csv('mandados_12_abr_2018_RJ.csv', sep=';')
all_lists = sum((literal_eval(l) for l in df.mandados.tolist()), [])
cols = ["nomeParte", "orgao", "numeroMandado", "dataMandado", "situacao", "detalhes"]
df = pd.DataFrame(all_lists)[cols]
Upvotes: 1