2017-11-20 6 views
0

수행합니다는 왜 두 개의 디스크가있는 PC가 팬더 파이썬 사용 디스크 공간

  • 110기가바이트 SSD를
  • 1TB HDD SSD를 무료 18기가바이트 주위에있다

.

아래의 파이썬 코드를 실행하면 내 SSD의 모든 공간을 "사용"합니다 (1GB 만 사용 가능). 이 코드는 폴더의 모든 SAS 파일을 반복하고 작업별로 그룹을 수행하며 각 파일의 결과를 하나의 큰 데이터 프레임에 추가합니다.

import pandas as pd 
import os 
import datetime 
import numpy as np 

#The function GetDailyPricePoints does the following: 
#1. Imports file 
#2. Creates "price" variable 
#3. Performs a group by 
#4. Decode byte variables and convert salesdate to date type (if needed) 

def GetDailyPricePoints(inpath,infile): 
    intable = pd.read_sas(filepath_or_buffer=os.path.join(inpath,infile)) 

    #Create price column 
    intable.loc[intable['quantity']!=0,'price'] = intable['salesvalue']/intable['quantity'] 
    intable['price'] = round(intable['price'].fillna(0.0),0) 

    #Create outtable 
    outtable = intable.groupby(["salesdate", "storecode", "price", "barcode"]).agg({'key_row':'count', 'salesvalue':'sum', 'quantity':'sum'}).reset_index().rename(columns = {'key_row':'Baskets', 'salesvalue':'Sales', 'quantity':'Quantity'}) 

    #Fix byte values and salesdate column 
    for column in outtable: 
     if not column in list(outtable.select_dtypes(include=[np.number]).columns.values): #loop non-numeric columns 
      outtable[column] = outtable[column].where(outtable[column].apply(type) != bytes, outtable[column].str.decode('utf-8')) 
     elif column=='salesdate': #numeric column and name is salesdate 
      outtable[column] = pd.to_timedelta(outtable[column], unit='D') + pd.Timestamp('1960-1-1') 

    return outtable 


inpath = r'C:\Users\admin\Desktop\Transactions' 
outpath = os.getcwd() + '\Export' 
outfile = 'DailyPricePoints' 

dirs = os.listdir(inpath) 
outtable = pd.DataFrame() 

#loop through SAS files in folder 
for file in dirs: 
    if file[-9:] == '.sas7bdat': 
     outtable.append(GetDailyPricePoints(inpath,file,decimals)) 

정확하게 디스크 공간을 사용하고 있는지 알고 싶습니다. 또한이 임시 작업이 저장되는 경로를 내 HDD의 경로로 변경하고 싶습니다.

+0

페이징 할 수 있습니까? – shanmuga

답변

1

RAM에 모든 데이터를 복사하고 있습니다. 이 경우에는 충분하지 않으므로 파이썬은 페이지 파일이나 가상 메모리를 대신 사용합니다. 이 문제를 해결할 수있는 유일한 방법은 더 많은 메모리를 확보하는 것입니다. 또는 하나의 큰 데이터 프레임에 모든 것을 저장할 수 없습니다. 각 파일을 outtable.to_pickle('csvfile.csv')이라는 피클에 씁니다. 당신은 하나의 큰 CSV에 모든 것을 저장 주장하는 경우

그러나, 당신은 첫 번째 인수로 파일 객체를 전달하여 CSV로 추가 할 수 있습니다 :

out = open('out.csv', 'a') 
outtable.to_csv(out, index = False) 

는 루프 내에서 .to_csv() 단계를 수행.

또한 데이터 프레임에 대한 .append() 메서드는 데이터 프레임을 제자리에서 수정하지 않고 대신 목록이있는 메서드와 달리 새 데이터 프레임을 반환합니다. 그래서 마지막 코드 블록은 아마도 여러분이 기대하는대로 행동하지 않을 것입니다.

+0

당신이 정확하게 하나의 큰 CSV에 모든 것을 저장한다고 가정 할 때 open ('out.csv', 'a') .. 메서드를 사용했습니다. –

+0

나는 생각했다. 최소한 페이지 파일이 저장된 하드 디스크를 변경하고 다른 드라이브를 사용하는 방법이 있습니까? –

+0

예. 이것은 접선 방향으로 진행되며 단계는 운영체제에 따라 다르지만 Google 검색에서 찾을 수 있습니다. 빠른주의 사항은 사람들이 대개 OS를 포함하지 않는 실제 드라이브에 페이지 파일을 가지고 있지만이 경우 OS 드라이브가 SSD (다른 하나는 그렇지 않은 경우)이므로 찬반 양론의 무게를 달기 위해 약간의 연구가 필요합니다. –