Home > Software design >  Subtracting specific values from column in pandas
Subtracting specific values from column in pandas

Time:05-01

I am new to pandas library.
I am working on a data set which looks like this :

enter image description here


suppose I want to subtract point score in the table.
I want to subtract 100 if the score in **point score** column if the score is below 1000
and subtract 200 if the score is above 1000 . How do I do this.
code :
import pandas as pd
df = pd.read_csv("files/Soccer_Football Clubs Ranking.csv")
df.head(4)

CodePudding user response:

Use:

np.where(df['point score']<1000, df['point score']-100, df['point score']-200) 

Demonstration:

test = pd.DataFrame({'point score':[14001,200,1500,750]})
np.where(test['point score']<1000, test['point score']-100, test['point score']-200)

Output:

enter image description here

Based on the comment:

temp = test[test['point score']<1000]
temp=temp['point score']-100
temp2 = test[test['point score']>=1000]
temp2=temp2['point score']-200
temp.append(temp2)

Another solution:

out = []
for cell in test['point score']:
    if cell < 1000:
        out.append(cell-100)
    else:
        out.append(cell-200)
test['res'] = out
  • Related