Create list with column contents

Asked

Viewed 2,142 times

1

Hello Please have a pandas.core.frame.Dataframe with these columns, in Python3:

Estado            150 non-null object
Cargo             150 non-null object
Nome_candidato    150 non-null object
CPF               150 non-null int64
Nome_urna         150 non-null object
Partido           150 non-null object
Situacao          150 non-null object
Avaliacao         130 non-null object
Projeto           150 non-null object
Link              72 non-null object
Autor_1           150 non-null object
Autor_1_limpo     150 non-null object
Autor_2           6 non-null object
Autor_2_limpo     6 non-null object
Autor_3           1 non-null object
Autor_3_limpo     1 non-null object
Autor_4           1 non-null object
Tipo              150 non-null object
Fonte             150 non-null object

I want to create a list only with the contents of the Project column. I did so:

projetos_eleitos = []
for i in autor1:
    valor_projeto = i.Projeto 
    projetos_eleitos.append([valor_projeto])

With that mistake:

AttributeError                            Traceback (most recent call last)
<ipython-input-32-c6845dcc0293> in <module>()
      1 for i in autor1:
----> 2     valor_projeto = i.Projeto
      3     projetos_eleitos.append([valor_projeto])
      4 

AttributeError: 'str' object has no attribute 'Projeto'

Does anyone know what the mistake is?

  • The field valor_projeto expecting a String, there is some property that can be called after object Project? Ex: valor_projeto = i.Projeto.nome or even perform a cast : valor_projeto = str(i.Projeto)

  • 1

    How is being defined autor1?

  • The error says: i is a String and the object String has no attribute called Projeto. How is being defined autor1 ?

  • Thank you all. Usei esse comando para pegar o conteúdo: valor_projeto = autor1.Projeto.tolist()&#xA;E autor1 estava definida assim: &#xA;autor1 = pd.merge(eleitos_d_p, proj_isa, left_on='Nome_urna', right_on='Autor_1_limpo')

2 answers

1


If you’re using pandas you don’t need to use a for...

If you just want to turn projects into a list, do it df.Projeto.tolist()

For example:

import pandas as pd

df = pd.DataFrame({'Autor': ['João', 'João', 'Maria', 'Maria', 'Joana'],
                   'Projeto': ['P'+str(i+1) for i in range(5)]})
print(df)

#    Autor Projeto
# 0   João      P1
# 1   João      P2
# 2  Maria      P3
# 3  Maria      P4
# 4  Joana      P5

print(df.Projeto.tolist())

['P1', 'P2', 'P3', 'P4', 'P5']

Now, if you want to group projects by author, you can generate a list of lists

df.groupby('Autor').apply(lambda grupo: grupo.Projeto.tolist()).tolist()

This returns [['P5'], ['P1', 'P2'], ['P3', 'P4']]

A disadvantage of this approach is that you lose the reference of the project author. In this case an option is to create a dictionary:

df.groupby('Autor').apply(lambda grupo: grupo.Projeto.tolist()).to_dict()

What generates {'Joana': ['P5'], 'João': ['P1', 'P2'], 'Maria': ['P3', 'P4']}

  • Thanks. So it worked: valor_projeto = autor1.Projeto.tolist()

0

Maybe this will help:

projetos_eleitos = []

for i in autor1:
    projetos_eleitos.append( i['valor_projeto'] )

Browser other questions tagged

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