Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using conditions in panda

this my data frame
my dataframe
i use this code to get something called WT

DF['T.U']=DF['WIDE']*DF['LENGTH']*DF['QTY']/1000000+DF['CLAD_THK']*DF['LENGTH']*DF['QTY']/1000000
    DF['U.W']=DF['THK']*78.5/10 + DF['CLAD_THK']*79.9/10
    DF['W.T']=DF['T.U']*DF['U.W']
    DF['W.T']=DF['WT']/1000

this is how data is after applying code
data frame after code
I want to add conditions depending on the MAT column to calculate WT for example if i have MAT=516, apply this code

 DF['T.U']=DF['WIDE']*DF['LENGTH']*DF['QTY']/1000000+DF['CLAD_THK']*DF['LENGTH']*DF['QTY']/1000000
 DF['U.W']=DF['THK']*78.5/10 + DF['CLAD_THK']*79.9/10
 DF['W.T']=DF['T.U']*DF['U.W']
 DF['W.T']=DF['WT']/1000

if MAT=240 apply this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DF['T.U']=DF['WIDE']*DF['LENGTH']*DF['QTY']/1000000+DF['CLAD_THK']*DF['LENGTH']*DF['QTY']/1000000
DF['U.W']=DF['THK']*79.9/10 + DF['CLAD_THK']*79.9/10
DF['W.T']=DF['T.U']*DF['U.W']
DF['W.T']=DF['WT']/1000

>Solution :

You should iterate over rows on your dataframe:

for index, row in DF.iterows():
   if (row['MAT'] == 516):
       # Apply code 1 on row instead of DF
   elif (row['MAT'] == 240):
       # Apply code 2 on row instead of DF
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading