I am absolute beginner. I have a problem in slicing string in a Excel file using Python. My Excel file contains the following info:
Column 1:
ordercode
PMC11-AA1L1FAVWJA
PMC21-AA1A1CBVXJA
PMP11-AA1L1FAWJJ
PMP21-AA1A1FBWJJ
PMP23-AA1A1FA3EJ+JA
PTP31B-AA3D1HGBVXJ
PTC31B-AA3D1CGBWBJA
PTP33B-AA3D1HGB1JJ
I want to slice the string in column "ordercode" based on whether it is
"PMC11"/"PMC21"/"PMP21"/"PMP11"/"PMP23"/"PTP31B"/"PTP33B"/"PTC31B" at different position and save it in new column "pressurerange". In Excel I have used the below code and it worked fine:
=IF(OR(ISNUMBER(SEARCH("PMC11",A2)),ISNUMBER(SEARCH("PMC21",A2)),ISNUMBER(SEARCH("PMP11",A2)),ISNUMBER(SEARCH("PMP21",A2)),ISNUMBER(SEARCH("PMP23",A2))),MID(A2,11,2),MID(A2,12,2))
but in Python I used the below coding, and it didn't work properly.
Python Code:
import pandas as pd
#Assigning the worksheet to file
file="Stratification_worksheet.xlsx"
#Loading the spreadsheet
data= pd.ExcelFile(file)
#sheetname
print(data.sheet_names)
#loading the sheetname to df1
df=data.parse("Auftrag")
print(df)
#creating a new column preessurerange and slicing the pressure range from order code
for index,row in df.iterrows():
if "PMC11" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(10,12)
elif "PMC21" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(10,12)
elif "PMP11" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(10,12)
elif "PMP21" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(10,12)
elif "PMP23" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(10,12)
elif "PTP31B" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(11,13)
elif "PTP33B" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(11,13)
elif "PTC31B" in df.loc[index,"ordercode"]:
df["pressurerange"]=df["ordercode"].str.slice(11,13)
else:
df["pressurerange"]="NONE"
print(df.loc[:,["pressurerange"]])
break
Here what it does is it checked the first IF condition and it sliced the string at the position (10,12) for all the column. I know I have done mistake in the below code. But I don't know what is the exact code to use.
=df["pressurerange"]=df["ordercode"].str.slice(10,12)