Alka
Alka

Reputation: 267

Calculate new column in pandas based on certain criteria

I am trying to multiple a two df based on certain values.

data = {'a':10,'b':20,'c':30} . 

df1:

id,tag,factor
1,a ,20 
2,b ,22 
3,c ,25 

my final result should look like something below:

id,factor,calc  
1,20,200
2,22,440
3,24,750

The steps I have tried but giving error.

df['calc'] = df['factor'] * data.get(df['tag'])

Upvotes: 2

Views: 56

Answers (5)

WebDev
WebDev

Reputation: 1371

Using list comprehension

df1['calc'] = [x.factor * data[x.tag] for i, x in df1.iterrows()]

Output is

   id tag  factor  calc
0   1   a      20   200
1   2   b      22   440
2   3   c      25   750

Upvotes: 1

Mateus Preste
Mateus Preste

Reputation: 1568

df['calc'] = df['factor'] * pd.Series([data[key] for key in df["tag"]])

OUTPUT

   id  factor  calc
0   1      20   200
1   2      22   440
2   3      25   750

Upvotes: 1

BENY
BENY

Reputation: 323276

In your case

df1['calc']=df1.factor*df1.tag.map(data)

Or do something different

df['calc']=df.factor*np.vectorize(data.get)(df.tag)#pd.Series(data).get(df.tag).values

Upvotes: 1

Ricky Kim
Ricky Kim

Reputation: 2022

You can use pandas.Series.map and pandas.DataFrame.drop

df['calc'] = df['factor'] * df['tag'].map(data)
df.drop('tag', axis=1, inplace=True)

OUTPUT:

   id  factor  calc
0   1      20   200
1   2      22   440
2   3      25   750

Upvotes: 1

Juan C
Juan C

Reputation: 6132

df1['calc']=df1.factor*df1.tag.map(data)

df1.tag.map(data) check the data dictionary values and replaces them by the associated key, thus giving you a new Series that can be multiplied by df1.factor

Upvotes: 1

Related Questions