Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

674
Visualizações
Python - ValueError: Cannot index with vector containing NA / NaN values

I'm trying to get the average price of products containing any substrings from a wordlist from a dataframe. I've been able to do so with the following code on multiple spreadsheets -

dframe['Product'].fillna('', inplace=True)
dframe['Price'].fillna(0, inplace=True)
total_count = 0
total_price = 0
for word in ransomware_wordlist:
    mask = dframe.Product.str.contains(word, case=False)
    total_count += mask.sum()
    total_price += dframe.loc[mask, 'Price'].sum()
average_price = total_price / total_count
print(average_price)

However, one of the spreadsheets throws an error at line -

dframe['Product'].fillna('', inplace=True)

with

ValueError: cannot index with vector containing NA / NaN values

I fail to understand why dframe['Product'].fillna('', inplace=True) isn't handling this problem.

In desperate need of some help! Thanks!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If first line failed still is possible replace NaNs in condition in str.contains by parameter na=False:

mask = dframe.Product.str.contains(word, case=False, na=False)

Or try omit inplace=True and assign back:

dframe['Product'] = dframe['Product'].fillna('')
over 4 years ago · Santiago Trujillo Relatório

0

One way to solve issues with index is to use actually use index:

# define x
x = "Price"

# make sure to fill Na/NaN values
dframe[x] = dframe[x].fillna('00')

# identify rows that contain a specific value, returns a list of True/False
id_rows = dframe[x].str.contains(r"^ransom")

# save row index for identified rows, basically save all True
row_index = dframe.loc[id_rows].index

# update the chosen variable `x` with new value on identified rows using row index
dframe.loc[row_index, x] = 'cleaned'

Does this trick!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda