Multiply every 2nd row by -1 in pandas col

Viewed 118

I'm trying to multiply every 2nd row by -1 but in a specified column only. Using below, I'm hoping to multiply every 2nd row in column c by -1.

df = pd.DataFrame({  
    'a' : [2.0,1.0,3.5,2.0,5.0,3.0,1.0,1.0],
    'b' : [1.0,-1.0,3.5,3.0,4.0,2.0,3.0,2.0],     
    'c' : [2.0,2.0,2.0,2.0,-1.0,-1.0,-2.0,-2.0],                  
    })

df['c'] = df['c'][::2] * -1

Intended output:

     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0
5 Answers

One way using pandas.DataFrame.update:

df.update(df['c'][1::2] * -1)
print(df)

Output:

     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0

Use DataFrame.iloc for slicing with Index.get_loc for position of column c:

df.iloc[1::2, df.columns.get_loc('c')] *= -1
#working same like
#df.iloc[1::2, df.columns.get_loc('c')] = df.iloc[1::2, df.columns.get_loc('c')] * -1

Or use DataFrame.loc with select values in df.index:

df.loc[df.index[1::2], 'c'] *= -1

Or:

df.loc[df.index % 2 == 1, 'c'] *= -1

print (df)
    
     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0

Or you can write your own function:

def multiple(df):
    new_df = pd.DataFrame()
    for i in range(0, len(df)):
        if i // 2 == 0:
            new_row = pd.DataFrame(data = df.iloc[i]*(-1)).T      
            new_df = new_df.append(new_row, ignore_index=True)
        else:
            new_row = pd.DataFrame(data = df.iloc[i]).T      
            new_df = new_df.append(new_row, ignore_index=True)            

        i+=1
        
    return new_df

You can use divmod with a series:

s = 2*np.arange(len(df))%2 - 1
df["c"] = -df.c*s

     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0

You can use this code :

     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0  2.0
2  3.5  3.5  2.0
3  2.0  3.0  2.0
4  5.0  4.0 -1.0
5  3.0  2.0 -1.0
6  1.0  3.0 -2.0
7  1.0  2.0 -2.0
df.loc[df.index % 2 == 1, "c" ] = df.c * - 1
     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0
Related