Doubt beginner ! Manipulating series

Asked

Viewed 51 times

1

I’m doing my first analysis with Pandas in Python 3. I’m practicing with a game of Thrones dataset that contains the deaths in the books.

One of the columns of the dataframe refers to the house and in this column there are repeated values of type: Stark / House Stark.

I would like to know how to remove the word "home" from that column.

Can someone please help me ?

1 answer

1


You can use the function replace:

str = "Casa Stark"
str = str.replace("Casa ", "")
print(str) #Retorna Stark

You have to use inside a loop, you can do something like this:

array = [
  ["coluna 1", "coluna 2", "X"],
  ["coluna 1", "coluna 2", "Stark"],
  ["coluna 1", "coluna 2", "Casa Stark"],
  ["coluna 1", "coluna 2", "Casa Z"],
  ["coluna 1", "coluna 2", "Casa Stark"],
]

for elemento in array:
  elemento[2] = elemento[2].replace("Casa ", "")
  print(elemento[2])
  • To do this can also be used list comprehensions, something like arrayCorrigido = [[element[0],element[1],element[2]. replace("Home ","")] for element in array]

Browser other questions tagged

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