developer tip

고유하지 않은 열의 날짜별로 Pandas DataFrame 항목을 그룹화하는 방법

optionbox 2020. 11. 10. 08:02
반응형

고유하지 않은 열의 날짜별로 Pandas DataFrame 항목을 그룹화하는 방법


Pandas DataFrame에는 "date"고유하지 않은 datetime을 포함하는 이름 지정된 열 이 있습니다 . 다음을 사용하여이 프레임의 라인을 그룹화 할 수 있습니다.

data.groupby(data['date'])

그러나 이렇게하면 데이터가 datetime값으로 분할 됩니다. "날짜"열에 저장된 연도별로 이러한 데이터를 그룹화하고 싶습니다. 이 페이지 는 타임 스탬프가 색인으로 사용되는 경우 연도별로 그룹화하는 방법을 보여줍니다. 제 경우에는 그렇지 않습니다.

이 그룹을 어떻게 달성합니까?


pandas 0.16.2를 사용하고 있습니다. 이것은 내 대규모 데이터 세트에서 더 나은 성능을 제공합니다.

data.groupby(data.date.dt.year)

dt옵션을 사용하고 weekofyear, dayofweek등을 가지고 노는 것이 훨씬 쉬워집니다.


ecatmur의 솔루션이 잘 작동합니다. 그러나 이는 대규모 데이터 세트에서 더 나은 성능을 제공합니다.

data.groupby(data['date'].map(lambda x: x.year))

이는 샘플 데이터 세트로 설명하기 더 쉬울 수 있습니다.

샘플 데이터 생성

Timestamps의 단일 열과 date집계를 수행하려는 다른 열인 a.

df = pd.DataFrame({'date':pd.DatetimeIndex(['2012-1-1', '2012-6-1', '2015-1-1', '2015-2-1', '2015-3-1']),
                   'a':[9,5,1,2,3]}, columns=['date', 'a'])

df

        date  a
0 2012-01-01  9
1 2012-06-01  5
2 2015-01-01  1
3 2015-02-01  2
4 2015-03-01  3

연도별로 그룹화하는 방법에는 여러 가지가 있습니다.

  • year속성 과 함께 dt 접근 자 사용
  • 넣어 date인덱스에 액세스 년에 익명 함수를 사용
  • 사용 resample방법
  • 판다 시대로 전환

.dt에 접근 year부동산

Pandas Timestamps의 열 (색인이 아님)이있는 경우 접근자를 사용하여 더 많은 추가 속성 및 메서드에 액세스 할 수 있습니다 dt. 예를 들면 :

df['date'].dt.year

0    2012
1    2012
2    2015
3    2015
4    2015
Name: date, dtype: int64

이를 사용하여 그룹을 구성하고 특정 열에 대한 일부 집계를 계산할 수 있습니다.

df.groupby(df['date'].dt.year)['a'].agg(['sum', 'mean', 'max'])

      sum  mean  max
date                
2012   14     7    9
2015    6     2    3

색인에 날짜를 넣고 익명 기능을 사용하여 연도에 액세스

If you set the date column as the index, it becomes a DateTimeIndex with the same properties and methods as the dt accessor gives normal columns

df1 = df.set_index('date')
df1.index.year

Int64Index([2012, 2012, 2015, 2015, 2015], dtype='int64', name='date')

Interestingly, when using the groupby method, you can pass it a function. This function will be implicitly passed the DataFrame's index. So, we can get the same result from above with the following:

df1.groupby(lambda x: x.year)['a'].agg(['sum', 'mean', 'max'])

      sum  mean  max
2012   14     7    9
2015    6     2    3

Use the resample method

If your date column is not in the index, you must specify the column with the on parameter. You also need to specify the offset alias as a string.

df.resample('AS', on='date')['a'].agg(['sum', 'mean', 'max'])

             sum  mean  max
date                       
2012-01-01  14.0   7.0  9.0
2013-01-01   NaN   NaN  NaN
2014-01-01   NaN   NaN  NaN
2015-01-01   6.0   2.0  3.0

Convert to pandas Period

You can also convert the date column to a pandas Period object. We must pass in the offset alias as a string to determine the length of the Period.

df['date'].dt.to_period('A')

0   2012
1   2012
2   2015
3   2015
4   2015
Name: date, dtype: object

We can then use this as a group

df.groupby(df['date'].dt.to_period('Y'))['a'].agg(['sum', 'mean', 'max'])


      sum  mean  max
2012   14     7    9
2015    6     2    3

This should work:

data.groupby(lambda x: data['date'][x].year)

this will also work

data.groupby(data['date'].dt.year)

참고URL : https://stackoverflow.com/questions/11391969/how-to-group-pandas-dataframe-entries-by-date-in-a-non-unique-column

반응형