2017-12-27 18 views
1

데이터 프레임을 만들고 apply (type)/applymap (type)을 다른 방법으로 호출했습니다. 문제는 다른 결과가 있다는 것입니다. intint64 유형에 대해 혼란스러워합니다.팬더에 왜 적용 (유형) 결과가 일치하지 않습니까?

In [116]: df_term[0:5] 
Out[116]: 
        term tag count weight   pt 
0     -03 OTHER 380 3085.0 2017-12-06 
1     -300 NUM 1224 6120.0 2017-12-06 
2     -805 OTHER  30 258.0 2017-12-06 
3 0-150mm0-200mm0-300mm  XH  27 1650.0 2017-12-06 
4     040639 OTHER  52 464.0 2017-12-06 

In [106]: df_term.dtypes 
Out[106]: 
term  object 
tag  object 
count  int64 
weight float64 
pt   object 
dtype: object 

In [109]: type(df_term.iloc[0]['count']) 
Out[109]: numpy.int64 

In [111]: df_term.iloc[0].apply(type)['count'] 
Out[111]: numpy.int64 

In [113]: type(df_term['count'].iloc[0]) 
Out[113]: numpy.int64 

In [114]: df_term['count'].apply(type)[0] 
Out[114]: int 

In [115]: df_term[0:1].applymap(type)['count'] 
Out[115]: 
0 <type 'int'> 
Name: count, dtype: object 

또한 자신의 유형을 비교하는 시도 :

In [156]: df_term.iloc[0].apply(type)['count'] 
Out[156]: numpy.int64 

In [157]: df_term.applymap(type).iloc[0]['count'] 
Out[157]: int 

In [158]: df_term.iloc[0].apply(type)['count'] == df_term.applymap(type).iloc[0]['count'] 
Out[158]: False 
+0

당신이 붙여 넣을 수 있습니다 이유 apply 수신 기능 (이 경우 type에 각 행의 값을 전달하기 위해, Series.apply 안에이 같은 무언가를 'df_term'의 처음 5 행? –

+0

@ cᴏʟᴅsᴘᴇᴇᴅ, 내용을 붙여 넣었습니다 – moshangcheng

+0

아, 지금 상황이 꽤 분명합니다. '팬더'가 표시하는 것과 IPython이 보여주는 것과는 차이가 있습니다. 그러나 둘 다 동일합니다. –

답변

1

간단한 예를 고려 -

In [13]: x = 5 

In [14]: type(x) 
Out[14]: int 

In [15]: repr(type(x)) 
Out[15]: "<class 'int'>" 

이 첫 번째 출력은 무엇 type 반환 IPython의 prettification입니다. 두 번째 출력은 동일한 출력의 __repr__이며 팬더가 보여주는 것입니다.

본질적으로 둘 다 똑같습니다. 당신은 명시 적으로 IPython.lib에서 그것을 가져 행동 IPython 's의 매우-프린터를 볼 수 있습니다 - int 및 표시되는 np.int64의 차이에 관한

s = pd.Series([1, 2, 3, 4]) 
s.apply(type) 

0 <class 'int'> 
1 <class 'int'> 
2 <class 'int'> 
3 <class 'int'> 
dtype: object 
from IPython.lib.pretty import pretty 

for r in s.apply(type): 
    print(pretty(r)) 

int 
int 
int 
int 

, 고려 -

In [16]: df.loc[0, 'count'] 
Out[16]: 380 

In [17]: type(df.loc[0, 'count']) 
Out[17]: numpy.int64 

In [18]: type(df.loc[0, 'count'].item()) 
Out[18]: int 

데이터는 기본적으로 데이터 프레임 열에 0으로로드됩니다개체. 특정 요소를 인덱스로 액세스하면 항상 numpy 객체가 반환되며 numpy 객체에서 .item()을 호출하여 파이썬 객체로 전송할 수 있습니다. 내 믿음은 암시 적으로 apply 당신이 <class 'int'>을보고하지 <class 'np.int64'>.이

+0

그 유형을 비교하려고했는데 그것들은 동일하지 않습니다. – moshangcheng

+0

@moshangcheng 맞아요, 그 차이점에 대해서는 제 생각에 데이터가 팬더 열에 기본적으로 numpy 객체로로드됩니다 (performanc e reason) df.loc [0, 'count']를했다면'int64' 객체가 반환된다는 것을 알 수있다. 이제 df.loc [0, 'count']. item()'을 호출하면 _int_ 객체가 반환됩니다. 내 생각 엔이 적용은 내재적으로 적용되는 본문 내에서 항목을 '형식'으로 전달하는 이와 같은 것을 수행한다는 것입니다. –