如何创建一个可以打印出销售利润和剩余库存的程序?

时间:2019-03-29 02:58:13

标签: python pandas dataframe

The merge.xlsx file here 提到excel文件“ merge.xlsx”,我的问题要求我比较购买和“销售”的库存。如果最新的存货不足以满足特定时间“销售”中所要求的数量,则程序应自动跳过该存货,然后转到下一个可行的存货。

这是使用遮罩方法对项目进行的排序。

import pandas as pd
pd.options.display.max_rows = 12
xlsx = pd.ExcelFile('merge.xlsx')
pd.set_option('display.max_columns', 6)
print(type(xlsx))
print(xlsx.sheet_names)
sr = [1, 2] 

sheets = [] 
for i, s in enumerate(xlsx.sheet_names): 
    sheets += [ xlsx.parse(s, skiprows=sr[i]) ]

for s in sheets: 
    s['item name'] = s['item name'].apply(str.strip)

mask = ( sheets[0]['item name'] == 'apple' )
df_apple = sheets[0][ mask ] 
mask2 = (sheets[1]['item name'] == 'apple')
df2_apple = sheets[1][mask2]

total_apple = pd.concat([df_apple, df2_apple], ignore_index = True, sort = False)
total_apple.sort_values(by = ['date/time'], inplace=True,ascending=True)
columns = ['date/time','item name','cost per unit','units 
purchased','price per unit','units sold']
total_apple.reindex(columns)

print(total_apple)

这是输出

January 2017:
    apple: remaining 2 items, profit of sales = $123.45
    orange: remaining 0 items, profit of sales = -$456.78
    durian: remaining 12 items, profit of sales = $789.01
    watermelon: remaining 6 items, profit of sales = $0.00

0 个答案:

没有答案