팬더의 크기와 개수의 차이점은 무엇입니까?
그게 팬더 groupby("x").count
와 의 차이점 groupby("x").size
입니까?
크기는 nil을 제외합니까?
size
NaN
값을 포함하고 다음을 포함 count
하지 않습니다.
In [46]:
df = pd.DataFrame({'a':[0,0,1,2,2,2], 'b':[1,2,3,4,np.NaN,4], 'c':np.random.randn(6)})
df
Out[46]:
a b c
0 0 1 1.067627
1 0 2 0.554691
2 1 3 0.458084
3 2 4 0.426635
4 2 NaN -2.238091
5 2 4 1.256943
In [48]:
print(df.groupby(['a'])['b'].count())
print(df.groupby(['a'])['b'].size())
a
0 2
1 1
2 2
Name: b, dtype: int64
a
0 2
1 1
2 3
dtype: int64
@Edchum의 답변에 약간만 추가하면 데이터에 NA 값이 없더라도 이전 예제를 사용하여 count ()의 결과가 더 장황합니다.
grouped = df.groupby('a')
grouped.count()
Out[197]:
b c
a
0 2 2
1 1 1
2 2 3
grouped.size()
Out[198]:
a
0 2
1 1
2 3
dtype: int64
팬더의 크기와 개수의 차이점은 무엇입니까?
다른 답변은 차이를 지적했지만 "N을 계산하고 그렇지 않다" 고 말하는 것은 완전히 정확 하지 않습니다. 실제로 NaN을 계산 하지만 실제로 는 호출 된 객체 의 크기 (또는 길이) 를 반환 한다는 사실의 결과입니다 . 당연히 여기에는 NaN 인 행 / 값도 포함됩니다.size
count
size
size
요약 size
하면 Series / DataFrame 1 의 크기를 반환합니다 .
df = pd.DataFrame({'A': ['x', 'y', np.nan, 'z']})
df
A
0 x
1 y
2 NaN
3 z
df.A.size
# 4
... count
비 NaN 값 을 계산하는 동안 :
df.A.count()
# 3
공지 size
특성을된다 (동일한 결과를 제공 len(df)
또는 len(df.A)
). count
함수입니다.
1. DataFrame.size
또한 속성이며 DataFrame (행 x 열)의 요소 수를 반환합니다.
동작 GroupBy
-출력 구조
기본적인 차이점 외에도 GroupBy.size()
vs를 호출 할 때 생성 된 출력의 구조에도 차이가 GroupBy.count()
있습니다.
df = pd.DataFrame({'A': list('aaabbccc'), 'B': ['x', 'x', np.nan, np.nan, np.nan, np.nan, 'x', 'x']})
df
A B
0 a x
1 a x
2 a NaN
3 b NaN
4 b NaN
5 c NaN
6 c x
7 c x
중히 여기다,
df.groupby('A').size()
A
a 3
b 2
c 3
dtype: int64
대,
df.groupby('A').count()
B
A
a 2
b 0
c 2
GroupBy.count
count
모든 열 을 호출하면 DataFrame 을 GroupBy.size
반환하고 Series 를 반환합니다.
The reason being that size
is the same for all columns, so only a single result is returned. Meanwhile, the count
is called for each column, as the results would depend on on how many NaNs each column has.
Behavior with pivot_table
Another example is how pivot_table
treats this data. Suppose we would like to compute the cross tabulation of
df
A B
0 0 1
1 0 1
2 1 2
3 0 2
4 0 0
pd.crosstab(df.A, df.B) # Result we expect, but with `pivot_table`.
B 0 1 2
A
0 1 2 1
1 0 0 1
With pivot_table
, you can issue size
:
df.pivot_table(index='A', columns='B', aggfunc='size', fill_value=0)
B 0 1 2
A
0 1 2 1
1 0 0 1
But count
does not work; an empty DataFrame is returned:
df.pivot_table(index='A', columns='B', aggfunc='count')
Empty DataFrame
Columns: []
Index: [0, 1]
I believe the reason for this is that 'count'
must be done on the series that is passed to the values
argument, and when nothing is passed, pandas decides to make no assumptions.
When we are dealing with normal dataframes then only difference will be an inclusion of NAN values, means count does not include NAN values while counting rows.
But if we are using these functions with the groupby
then, to get the correct results by count()
we have to associate any numeric field with the groupby
to get the exact number of groups where for size()
there is no need for this type of association.
참고URL : https://stackoverflow.com/questions/33346591/what-is-the-difference-between-size-and-count-in-pandas
'developer tip' 카테고리의 다른 글
부팅 할 시뮬레이터 장치를 확인할 수 없습니다. (0) | 2020.11.22 |
---|---|
LocalDB SQL Server 2014 Express는 2 개의 인스턴스 (localdb) \ ProjectsV12 & (localdb) \ MSSQLLocalDB? (0) | 2020.11.21 |
Visual Studio는 창 설정을 잊어 버리고 엉망이됩니다. (0) | 2020.11.21 |
JVM은 오픈 소스 코드입니까? (0) | 2020.11.21 |
/ Ox 및 / O2 컴파일러 옵션의 차이점은 무엇입니까? (0) | 2020.11.21 |