0

I have a dataframe object from pandas and I wanted to know if there is any way that I can access a specific value from a specific column and change it.

from pandas import DataFrame as df

gameboard = df([['#','#',"#"],['#','#',"#"],['#','#',"#"]], columns = [1, 2, 3], index = [1,2,3])
print(gameboard)

Like for example, I wanted to change the '#' from the second 'second' list. Or if gameboard was a 2d list how can I access gameboard[1][1]'s element.

lol troll
  • 11
  • 5
  • 1
    Take a look at, [How are iloc ix and loc differrent](https://stackoverflow.com/questions/31593201/how-are-iloc-ix-and-loc-different) – DOOM Mar 23 '20 at 15:31

1 Answers1

1

I think you're looking for the .iloc function (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html)

to access said value you would need to call something like:

gameboard.iloc[1, 1] = 6

iloc would essentially call the second row (that's what the [1 is) and then you choose the location of the value in the list (, 1] for the second value in our case). Finally you assign whatever new value you want that to be.

Your output would be:

    1   2   3
1   #   #   #
2   #   6   #
3   #   #   #

edit using alollz recommendation.

Gorlomi
  • 515
  • 2
  • 11