df = pd.DataFrame(np.zeros((6, 2)), columns=['A', 'B'])
I want to assign a value to the last value of column B
. In addition to the following two methods, is there an easier way?
df.at[df.index[-1], 'B'] = 1
df.loc[df.index[-1], 'B'] = 1
CodePudding user response:
If you know the index of column B
then,
df.iloc[-1,1] = 1
Else,
df.iloc[-1].B = 1
CodePudding user response:
This is a mixture of labeled indexing and indexed indexing, so I your solutions are nice already, but you could do:
df.iloc[-1, df.columns.get_loc('B')] = 1
Which is pretty neat, but it might be long but it's just the function name that's long.
Or why not just?
df.iloc[-1]['B'] = 1