简体   繁体   中英

deleting loops to increase efficiency in python

How do I make this more efficient? I feel like I should be able to do this without looping through the entire dataframe. Basically I have to split the column CollectType , into multiple columns depending on the the value in column SSampleCode .

for i in range(0,len(df)):
  if df.SSampleCode[i]=='Rock':
     df.R_SampleType[i]=df.CollectType[i]
  elif df.SSampleCode[i]=='Soil':
     df.S_SampleType[i]=df.CollectType[i]
  elif df.SSampleCode[i]=='Pan Con':
     df.PC_SampleType[i]=df.CollectType[i]
  elif df.SSampleCode[i]=='Silt':
     df.SS_SampleType[i]=df.CollectType[i]

This can be done using masks (vectorial approach):

for i in range(0,len(df)):
  if df.SSampleCode[i]=='Rock':
     df.R_SampleType[i]=df.CollectType[i]

will be

mask = df.SSampleCode=='Rock'
df.R_SampleType[mask] = df.CollectType[mask]

This will give you a good perf improvement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM