我有一个10,000行的csv数据文件,我从中读取并希望操作数据。我想循环,并以矩阵形式计算我的结果,这涉及采取特定列和进行计算。但每次我运行相同的程序,我得到不同的结果。我在这里做错了吗?我需要知道一些例外情况吗?我使用python 3.5。另外,建议是否有更好的方法,因为我是新手用熊猫分析数据。
# Import all important stuff
import numpy as np # imports a fast numerical programming library
import scipy as sp #imports stats functions, amongst other things
import matplotlib as mpl # this actually imports matplotlib
import matplotlib.cm as cm #allows us easy access to colormaps
import matplotlib.pyplot as plt #sets up plotting under plt
import pandas as pd #lets us handle data as dataframes
#sets up pandas table display
pd.set_option('display.width', 500)
pd.set_option('display.max_columns', 100)
pd.set_option('display.notebook_repr_html', True)
import seaborn as sb #sets up styles and gives us more plotting options
%matplotlib inline
# Read data file
RawData=pd.read_csv("C:/Users/User/../RawData.csv")
# Calculate volumetric sales - convert cubic.cm to cubic meter
RawData["Volumetric Sales"] = RawData["IUM L"] * RawData["IUM W"] * RawData["IUM H"] * (10**-6) * RawData["Sales past week"] /7
# For 0 sale items, reserve space for atleast 1 unit in Bartholdi's formula
RawData.loc[RawData["Volumetric Sales"] == 0, "Volumetric Sales"] = RawData["IUM L"] * RawData["IUM W"] * RawData["IUM H"] * (10**-6)
# Sort by volumetric sales and change the index
RawData=RawData.sort_values(by="Volumetric Sales", ascending=False)
RawData.index = range(1,len(RawData) + 1)
# Find cumulative % of volumetric sales
RawData["Cumulative Volumetric Sales"] = 100 * RawData["Volumetric Sales"].cumsum() / RawData["Volumetric Sales"].sum()
# Find IUM and PUM volumes
RawData["IUM Volume"] = RawData["IUM L"] * RawData["IUM W"] * RawData["IUM H"] * (10**-6)
RawData["PUM Volume"] = RawData["PUM L"] * RawData["PUM W"] * RawData["PUM H"] * (10**-6)
# Reference Data - Dry
DesignOrders = 1856
ProjectedOrders = [1856, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3400, 3600, 3800, 4000, 4200, 4400, 4600, 4800, 5000]
ReferenceDOS = 19.41
ProjectedDOS = [10, 12, 14, 16, 18, 19.41, 21]
# Total Volume of the Pick faces in cubic meters - to be changed
TotalPFVolume = 900
# Have a dummy dataframe for recording the volumes
VolumeProjections = pd.DataFrame(np.zeros((len(ProjectedOrders), len(ProjectedDOS))), index=ProjectedOrders, columns=ProjectedDOS)
# Loop over Order-DOS combinations
for Orders in ProjectedOrders:
for DOS in ProjectedDOS:
# Hold atleast 1 CS for every SKU, project it for DOS and Number of Orders
RawData["Quantity To Hold"] = np.maximum(RawData["Conv qty"], np.ceil((RawData["Sales past week"] / 7) * RawData["Long-term DOS"] * (DOS/ReferenceDOS) * (Orders/DesignOrders)))
# Calculate Bartholdi ratio (for calculating volume) for every SKU
RawData["Bang For Buck Volume"] = np.sqrt(RawData["Volumetric Sales"] * (DOS/ReferenceDOS) * (Orders/DesignOrders) * 365)
# Find total ratio
TotalRatio = RawData["Bang For Buck Volume"].sum()
# Replace ratio with actual BFB volume
RawData["Bang For Buck Volume"] = RawData["Bang For Buck Volume"] / TotalRatio * TotalPFVolume
# Find out 1.2 CS Volume
RawData["MinimumCSVolume"] = np.maximum(RawData["IUM Volume"] * 1.2 * RawData["Conv qty"], RawData["PUM Volume"])
# Find out minimum PF volume to be dedicated
RawData["MinimumPFVolume"] = np.maximum(RawData["Bang For Buck Volume"], RawData["MinimumCSVolume"])
VolumeProjections[DOS][Orders] = (RawData["Quantity To Hold"] * RawData["PUM Volume"] / RawData["Conv qty"]).sum()
print(VolumeProjections)
首次输出:
10.00 12.00 14.00 16.00 18.00 19.41 21.00
1856 461.470704 814.294089 498.952202 692.073162 767.460646 823.379664 899.881262
2000 490.722450 573.388032 654.591470 738.901730 125.450220 881.608444 949.107524
2200 488.448647 621.031090 902.686916 804.691009 898.690028 193.467034 1037.500241
2400 573.388032 671.466619 771.939732 872.041555 824.477296 1044.463665 1127.280933
2600 613.172775 721.686269 830.917824 942.274260 904.456359 1128.260223 199.670501
2800 654.591470 771.939732 888.837779 1007.957693 1127.280933 1210.356114 1303.793814
3000 697.014717 814.294089 762.973265 1076.826423 1203.418445 1291.381509 1393.277041
3200 738.901730 907.329916 814.294089 1142.877798 1279.231844 1374.000727 208.728851
3400 169.283162 922.709658 1068.259174 1211.165380 1356.711651 1458.048882 902.686916
3600 307.466789 974.235769 1127.280933 1279.231844 1430.845138 1539.624099 814.294089
3800 864.390648 1024.772862 806.131997 899.881262 1508.932226 1622.537921 1750.385991
4000 906.896879 814.294089 125.448108 1415.526353 902.686916 222.723561 246.031964
4200 949.107524 1032.639510 1303.793814 1483.680183 1662.568445 1788.231250 226.243325
4400 899.881262 296.567996 902.686916 1551.248439 814.294089 1872.211342 2021.472316
4600 1033.713507 1228.240414 1423.718589 1619.825535 1813.973271 1955.691168 2110.941821
4800 1076.826423 1279.231844 1483.680183 169.283162 1148116.618907 2036.863282 2202.442643
5000 1117.762804 1330.539197 1541.371825 1757.894289 1970.034712 2120.383575 2288.757769
第二次输出:
10.00 12.00 14.00 16.00 18.00 19.41 21.00
1856 461.470704 536.893794 613.039076 692.073162 169.283162 907.111078 885.086313
2000 428.238473 573.388032 654.591470 738.901730 814.294089 881.608444 175.021151
2200 531.236534 621.031090 169.283162 804.691009 898.690028 169.283162 1037.500241
2400 573.388032 671.466619 771.939732 872.041555 196.363519 1044.463665 902.686916
2600 902.686916 721.686269 830.917824 942.274260 1050.509828 902.686916 902.686916
2800 902.686916 771.939732 888.837779 1007.957693 1127.280933 1210.356114 202.436238
3000 697.014717 822.973094 949.107524 1076.826423 1203.418445 1291.381509 1393.277041
3200 738.901730 872.041555 1007.957693 1142.877798 1279.231844 1374.000727 1483.680183
3400 779.775957 922.709658 1068.259174 1211.165380 1356.711651 1458.048882 1574.101946
3600 822.973094 974.235769 1057.805643 1279.231844 909.019825 1539.624099 1662.568445
3800 864.390648 169.283162 1182.686040 1347.291081 814.294089 1622.537921 1750.385991
4000 906.896879 1076.826423 1244.883257 1415.526353 902.686916 814.294089 1841.840966
4200 949.107524 1127.280933 902.686916 902.686916 2351.239410 1788.231250 1930.603268
4400 991.031678 814.294089 1364.490160 1551.248439 814.294089 1872.211342 2021.472316
4600 1033.713507 1228.240414 1423.718589 169.283162 1813.973271 899.881262 902.686916
4800 1076.826423 1279.231844 1483.680183 1685.749648 1893.147718 237.532106 2202.442643
5000 1117.762804 1330.539197 241.441668 1757.894289 1970.034712 910.288608 2288.757769