-1

I have prepared the dataframe

Col1 Col2
0 a1 temp
1 b1 temp
2 c1 temp

I prepared the Col2 with df['Col2'] = 'temp'

I would like to overwrite only Col2 row0, and tried following code.

df['Col2'][0] = 'a'

However I got an below error.

IndexError: list index out of range

aaaaa0a
  • 127
  • 2
  • 13
  • `df.at[0, 'Col2']` or `df.iat[0, 1]`. Cf [pandas loc vs. iloc vs. at vs. iat?](https://stackoverflow.com/questions/28757389/pandas-loc-vs-iloc-vs-at-vs-iat). – keepAlive May 17 '21 at 07:49
  • You could also use `loc` try: `df.loc[0,'Col2']='new_Value'` to get 1st element of col1. – RavinderSingh13 May 17 '21 at 07:50
  • ```df['Col2'][0] = 'a'``` works for me –  May 17 '21 at 07:52
  • Your code df['Col2'][0] = 'a' works for me. The syntax is perfectly fine. So, one possible reason is your row index doesn't contain 0. In this case, you can use: df['Col2'].iloc[0] = 'a' if you want to modify for the first row. – SeaBean May 17 '21 at 08:03

2 Answers2

0

Please try using iloc function

df = pd.DataFrame({'col1':[1, 2, 3], 'col2':['temp', 'temp', 'temp']})
df.iloc[2:, 1:] = 'R'

supposed to work.

Note - there are many other ways too.

JSVJ
  • 500
  • 3
  • 14
0

You need to index by row first, then column using the .loc accessor:

df.loc[0, 'Col2'] = 'a'
Nick W
  • 146
  • 9