如何从python中的pandas更改/访问Dataframe中的特定值
我有一个来自pandas的dataframe对象,我想知道是否有任何方法可以从特定列访问特定值并对其进行更改如何从python中的pandas更改/访问Dataframe中的特定值,python,python-3.x,pandas,list,dataframe,Python,Python 3.x,Pandas,List,Dataframe,我有一个来自pandas的dataframe对象,我想知道是否有任何方法可以从特定列访问特定值并对其进行更改 from pandas import DataFrame as df gameboard = df([['#','#',"#"],['#','#',"#"],['#','#',"#"]], columns = [1, 2, 3], index = [1,2,3]) print(gameboard) 例如,我想从第二个“second”列表中更改“#”。 或者,如果gameboard是
from pandas import DataFrame as df
gameboard = df([['#','#',"#"],['#','#',"#"],['#','#',"#"]], columns = [1, 2, 3], index = [1,2,3])
print(gameboard)
例如,我想从第二个“second”列表中更改“#”。
或者,如果gameboard是一个2d列表,我如何访问gameboard[1][1]的元素。我想您正在寻找.iloc函数 () 要访问所述值,您需要调用以下内容:
gameboard.iloc[1, 1] = 6
iloc基本上会调用第二行(这就是[1
的含义),然后选择列表中值的位置(,1]
,在本例中,第二个值)。最后,您可以指定您想要的任何新值
您的输出将是:
1 2 3
1 # # #
2 # 6 #
3 # # #
使用alollz建议进行编辑。查看,