-1

I'm trying to input some values in this dataframe:

I have a list of columns name:

input: centro_oeste.columns
output: ['ADMINISTRAÇÃO', 'ANÁLISE E DESENVOLVIMENTO DE SISTEMAS',
       'ARTES CÊNICAS', 'AUTOMAÇÃO INDUSTRIAL', 'CIÊNCIAS BIOLÓGICAS',
       'CIÊNCIAS CONTÁBEIS', 'DIREITO', 'EDUCAÇÃO FÍSICA', 'ENFERMAGEM',
       'ENGENHARIA AMBIENTAL', 'ENGENHARIA DE SOFTWARE',
       'ENGENHARIA FLORESTAL', 'FISIOTERAPIA', 'FÍSICA', 'GESTÃO AMBIENTAL',
       'GESTÃO CONTÁBIL E TRIBUTÁRIA', 'HISTÓRIA', 'PEDAGOGIA', 'PSICOLOGIA',
       'SISTEMAS DE INFORMAÇÃO', 'ANO_INGRESSO'],
      dtype='object')

From that columns list, I created a new Dataframe:

input: slope_CentroOeste = pd.DataFrame(columns=[centro_oeste.columns])
slope_CentroOeste

output: Dataframe

And now, my goal is input some value inside this dataframe, so I try:

slope_CentroOeste.loc[0] = np.nan
slope_CentroOeste.ADMINISTRAÇÃO[0] = 'x'

And Python gives the error: only integer scalar arrays can be converted to a scalar index


And i tried too

slope_CentroOeste.loc[0,'ADMINISTRAÇÃO'] = 'x'

And Python gives the error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please, can anyone help me?

  • Is the column `ADMINISTRAÇÃO` supposed to be a string or a number? Also, if you want to set a value as `NaN` you either use `None` or `numpy.nan`. – xicocaio Mar 30 '21 at 19:55
  • Why are you creating a row separately. Wouldn't it be best to create the list first and then create the dataframe using that list? – Joe Ferndz Mar 30 '21 at 20:05
  • Please read through this Stack Overflow post for more details on appending to [dataframe](https://stackoverflow.com/questions/13784192/creating-an-empty-pandas-dataframe-then-filling-it/56746204#56746204) – Joe Ferndz Mar 30 '21 at 20:06
  • Sorry if the question got confused I added the string NaN in the line, but it could be anything else, my goal is to input a value in line 0 and in the column I want, but it is not working. – Luis Henrique Batista Mar 30 '21 at 20:20
  • @JoeFerndz thank you very much, I will read this – Luis Henrique Batista Mar 30 '21 at 20:26

1 Answers1

0

I ran your code in this order and all worked well.

list_of_columns = ['ADMINISTRAÇÃO', 'ANÁLISE E DESENVOLVIMENTO DE SISTEMAS',
   'ARTES CÊNICAS', 'AUTOMAÇÃO INDUSTRIAL', 'CIÊNCIAS BIOLÓGICAS',
   'CIÊNCIAS CONTÁBEIS', 'DIREITO', 'EDUCAÇÃO FÍSICA', 'ENFERMAGEM',
   'ENGENHARIA AMBIENTAL', 'ENGENHARIA DE SOFTWARE',
   'ENGENHARIA FLORESTAL', 'FISIOTERAPIA', 'FÍSICA', 'GESTÃO AMBIENTAL',
   'GESTÃO CONTÁBIL E TRIBUTÁRIA', 'HISTÓRIA', 'PEDAGOGIA', 'PSICOLOGIA',
   'SISTEMAS DE INFORMAÇÃO', 'ANO_INGRESSO']

slope_CentroOeste = pd.DataFrame(columns=list_of_columns)

First test:

slope_CentroOeste.ADMINISTRAÇÃO[0] = 'x'
slope_CentroOeste

Second test:

slope_CentroOeste.loc[0] = 'NaN'
slope_CentroOeste

Third test:

slope_CentroOeste.loc[0,'ADMINISTRAÇÃO'] = 'x'
slope_CentroOeste

Last output (of third test):

           ADMINISTRAÇÃO    ANÁLISE E DESENVOLVIMENTO DE SISTEMAS       ...
    0              x                                         NaN        ...

Note that once you set a value for any entry of row 0, all columns of that row will have NaN in them automatically.