Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

pandas - Selecting cells from excel with multiple conditions

I am new to python and trying to use it to help reduce manual work with spreadsheets.

I have a spread sheet with a part number column containing integers e.g. 9326451, another column for probability which is a percentage in excel of either 100%, 80%, 50%, 20% and 0% which come into my python data frames as float values, there are then several columns with headers for each month that show the number of parts for each month, these are also float values due to rounding errors with Excel.

I have managed to write a program that can read the excel data using pandas. What I am trying to achieve is to create a data frame from the spreadsheet that separates the data in the spreadsheet by part number and probability. I have manged to do this for the part numbers successfully but when I also try to add a condition for the probability I am not getting what I want.

This returns the part numbers I want and totals the columns I want but doesn't filter the probability:

newdf = df.loc[(df["PN"] == 9125358) | (df["LP PN"] == 9355853), "Vol Dec 20":"Vol Dec 23"]
newdf.loc['Type1_Total'] = newdf.sum(axis = 0)
newdf_100.head()

When I try to add a condition for the Probability it doesn't seem to filter the data?

newdf = df.loc[(df["PN"] == 9125358) | (df["LP PN"] == 9355853) & (df["Probability"] == 1.0), "Vol Dec 20":"Vol Dec 23"]
newdf.loc['Type1_Total'] = newdf.sum(axis = 0)
newdf_100.head()

I would appreciate any help or support you can offer

Thanks,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...