Home > Back-end >  How to replace specific index to 0?
How to replace specific index to 0?

Time:12-28

Here is my following code. Currently, I write like this.

aaa = []
#length is a dataframe
for note in range(0, 20)):
    target = length["columns"][note]
    aaa.append(note)
    
    if target != 0.5:
        aaa[note] == 0
    df_ls = length.iloc[aaa]
df_ls

aaa[note] have these index ([4, 5, 6, 7, 8, 13, 14, 16, 21, 26, 29, 31])

but still aaa[note] in df_ls not equal 0 I want index [4, 5, 6, 7, 8, 13, 14, 16, 21, 26, 29, 31] value change to 0.

I know it would be an easy question, but I can’t get it!

CodePudding user response:

You wrote aaa[note] == 0 instead of just =.

CodePudding user response:

It worked!

aaa = []
A=[]
for note in range(0, 20)):
    target = length["columns"][note]
    aaa.append(note)
    
    if target != 0.5:
        A.append(aaa[note])

    df_ls = length.iloc[aaa]
for i in A:
    length.iloc[i,1:41]=0
length
  • Related