2017-11-07 9 views
1

에서 원인 팬더 오브젝트와 같은 모양 : 난 그냥 값 P4를 얻을 수 및 발행 수의 수를 플롯하려고큰 날짜 값과 내가 (3백메가바이트 ~) 큰 txt 파일이 메모리

df= pd.read_csv('file.txt') 
df.head() 

    <Base> <DTYYYYMMDD> <TIME> <p1> <p2> <p3> <p4> <q> 
36 x  20010102 235700 0.5622 0.5622 0.5622 0.5622 4 
37 x  20010102 235800 0.5622 0.5622 0.5622 0.5622 4 
38 x  20010102 235900 0.5622 0.5622 0.5622 0.5622 4 
39 x  20010103 0  0.5618 0.5618 0.5618 0.5618 4 
40 x  20010103 300  0.5622 0.5622 0.5622 0.5622 4 
41 x  20010103 500  0.5622 0.5622 0.5622 0.5622 4 

df.shape() 

(5560000, 8) 

다른 해에는 매년. 이를 위해 내가 먼저 (그들은 텍스트 파일에서 정수로 읽습니다) 문자열로 DTYYYYMMDD 및 시간 필드를 변환하려고 다음과 같이 다음 날짜로 변환 : 난 그냥 날짜 부분을 얻기 위해 노력하고

이제
datestr = df['<DTYYYYMMDD>'].apply(lambda x: str(x)) 
timestr = df['<TIME>'].apply(lambda x: str(x)) 
zeros = timestr.apply(lambda x: '0' * (6- len(x))) 
timestr = zeros + timestr 
dtstr = datestr + timestr 
p4_df = df['<p4>'] 
dt_datetime = pd.to_datetime(dtstr, format = '%Y%m%d%H%M%S') 
p4_df.index = dt_datetime 

따로 따로 그룹으로 나눌 수있어 나는 다른 계산에서 필요하기 때문에 전체 datetime 인덱스도 유지해야합니다.

p4_df['Date'] = dt_datetime.apply(lambda x: x.date()) 
to_plot = p4_df.groupby(['Date'])['<p4>'].count() 
to_plot.plot() 

dt_datetime.apply 라인에 메모리 오류가 발생합니다. 나는 다음과 같이 대신 오류를 시도했다.

p4_df['Date'] = pd.to_datetime(datestr, format = '%Y%m%d') 

코드를보다 효율적으로 사용하기위한 제안이 있으십니까?

답변

1

당신은 문자열로 변환하기위한 astype을 필요로하고 zfill으로 0을 추가 date

#if dont need omit NaNs use size instaed count 
to_plot = df.groupby(df.index.floor('D'))['<p4>'].count() 
to_plot.plot() 

또는 사용 :

dtstr = df['<DTYYYYMMDD>'].astype(str) + df['<TIME>'].astype(str).str.zfill(6) 
df.index = pd.to_datetime(dtstr, format = '%Y%m%d%H%M%S') 
print (df) 
        <Base> <DTYYYYMMDD> <TIME> <p1> <p2> <p3> \ 
2001-01-02 23:57:00  x  20010102 235700 0.5622 0.5622 0.5622 
2001-01-02 23:58:00  x  20010102 235800 0.5622 0.5622 0.5622 
2001-01-02 23:59:00  x  20010102 235900 0.5622 0.5622 0.5622 
2001-01-03 00:00:00  x  20010103  0 0.5618 0.5618 0.5618 
2001-01-03 00:03:00  x  20010103  300 0.5622 0.5622 0.5622 
2001-01-03 00:05:00  x  20010103  500 0.5622 0.5622 0.5622 

         <p4> <q> 
2001-01-02 23:57:00 0.5622 4 
2001-01-02 23:58:00 0.5622 4 
2001-01-02 23:59:00 0.5622 4 
2001-01-03 00:00:00 0.5618 4 
2001-01-03 00:03:00 0.5622 4 
2001-01-03 00:05:00 0.5622 4 

또 다른 더 나은 성능을 dates을 위해 사용 DatetimeIndex.floor 경우 얻을

to_plot = df.groupby(df.index.date)['<p4>'].count() 
to_plot.plot() 

또 다른 아이디어는 다음 string로 변환 만 <DTYYYYMMDD>를 사용하지 않아도된다 :

df.index = pd.to_datetime(df['<DTYYYYMMDD>'], format = '%Y%m%d') 
print (df) 
      <Base> <DTYYYYMMDD> <TIME> <p1> <p2> <p3> <p4> <q> 
<DTYYYYMMDD>                 
2001-01-02  x  20010102 235700 0.5622 0.5622 0.5622 0.5622 4 
2001-01-02  x  20010102 235800 0.5622 0.5622 0.5622 0.5622 4 
2001-01-02  x  20010102 235900 0.5622 0.5622 0.5622 0.5622 4 
2001-01-03  x  20010103  0 0.5618 0.5618 0.5618 0.5618 4 
2001-01-03  x  20010103  300 0.5622 0.5622 0.5622 0.5622 4 
2001-01-03  x  20010103  500 0.5622 0.5622 0.5622 0.5622 4 

to_plot = df.groupby(level=0)['<p4>'].count() 
print (to_plot) 
<DTYYYYMMDD> 
2001-01-02 3 
2001-01-03 3 
Name: <p4>, dtype: int64 

는 EDIT1 : 더 나은 성능을 문자열에 의해 처음으로 집계하고 다음 날짜에 작은 집계 출력 변환해야합니다 :

to_plot = df.groupby('<DTYYYYMMDD>')['<p4>'].count() 
to_plot.index = pd.to_datetime(to_plot.index, format = '%Y%m%d') 
print (to_plot) 
<DTYYYYMMDD> 
2001-01-02 3 
2001-01-03 3 
Name: <p4>, dtype: int64 

EDIT2 :

다른 코드에서 변수를 사용해야하는 경우 :

datestr = df['<DTYYYYMMDD>'].astype(str) 
timestr = df['<TIME>'].astype(str).str.zfill(6) 

dtstr = datestr + timestr 

p4_df = df['<p4>'] 
dt_datetime = pd.to_datetime(dtstr, format = '%Y%m%d%H%M%S') 
p4_df.index = dt_datetime 

p4_df['Date'] = dt_datetime.date() 
to_plot = p4_df.groupby(['Date'])['<p4>'].count() 
to_plot.plot() 
+1

감사합니다. – dayum