groupby 개체를 인쇄하는 방법
Pandas로 그룹화 한 결과를 인쇄하고 싶습니다.
데이터 프레임이 있습니다.
import pandas as pd
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three', 'three', 'one'], 'B': range(6)})
print(df)
A B
0 one 0
1 one 1
2 two 2
3 three 3
4 three 4
5 one 5
'A'로 그룹화 한 후 인쇄 할 때 다음이 있습니다.
print(df.groupby('A'))
<pandas.core.groupby.DataFrameGroupBy object at 0x05416E90>
그룹화 된 데이터 프레임을 어떻게 인쇄 할 수 있습니까?
만약 내가한다면:
print(df.groupby('A').head())
그룹화되지 않은 것처럼 데이터 프레임을 얻습니다.
A B
A
one 0 one 0
1 one 1
two 2 two 2
three 3 three 3
4 three 4
one 5 one 5
나는 다음과 같은 것을 기대하고 있었다.
A B
A
one 0 one 0
1 one 1
5 one 5
two 2 two 2
three 3 three 3
4 three 4
간단하게 :
grouped_df = df.groupby('A')
for key, item in grouped_df:
print(grouped_df.get_group(key), "\n\n")
이것도 작동합니다.
grouped_df = df.groupby('A')
gb = grouped_df.groups
for key, values in gb.iteritems():
print(df.ix[values], "\n\n")
선택적 키 그룹화의 경우 :key_list_from_gb
다음을 사용하여, 안에 원하는 키를 삽입합니다 gb.keys()
. 예 :
gb = grouped_df.groups
gb.keys()
key_list_from_gb = [key1, key2, key3]
for key, values in gb.items():
if key in key_list_from_gb:
print(df.ix[values], "\n")
단순히 표시하는 방법을 찾고 있다면 describe ()를 사용할 수 있습니다.
grp = df.groupby['colName']
grp.describe()
이것은 당신에게 깔끔한 테이블을 제공합니다.
head()
버전 0.12와 0.13 사이 의 변경 동작을 확인했습니다 . 그것은 나에게 버그처럼 보입니다. 문제를 만들었습니다 .
But a groupby operation doesn't actually return a DataFrame sorted by group. The .head()
method is a little misleading here -- it's just a convenience feature to let you re-examine the object (in this case, df
) that you grouped. The result of groupby
is separate kind of object, a GroupBy
object. You must apply
, transform
, or filter
to get back to a DataFrame or Series.
If all you wanted to do was sort by the values in columns A, you should use df.sort('A')
.
Another simple alternative:
for name_of_the_group, group in grouped_dataframe:
print (name_of_the_group)
print (group)
Also, other simple alternative could be:
gb = df.groupby("A")
gb.count() # or,
gb.get_group(your_key)
참고URL : https://stackoverflow.com/questions/22691010/how-to-print-a-groupby-object
'Programming' 카테고리의 다른 글
2 세트의 조합은 모든 항목을 포함하지 않습니다. (0) | 2020.08.28 |
---|---|
파이썬 인쇄 방법 : '형식'또는 퍼센트 형식? (0) | 2020.08.28 |
요구 사항 tensorflow를 충족하는 버전을 찾을 수 없습니다. (0) | 2020.08.28 |
설정할 자바 스크립트 배열 (0) | 2020.08.28 |
AngularJS에서 격리 된 범위없이 지시문에서 컨트롤러 함수 호출 (0) | 2020.08.28 |