본문 바로가기

hash3

Python 데이터프레임 열에서 값이 발생하는 빈도를 계산하십시오., Count the frequency that a value occurs in a dataframe column 질문 저는 데이터셋을 가지고 있습니다. category cat a cat b cat a 다음과 같이 고유한 값과 그 빈도를 보여주는 결과를 반환하고 싶습니다. category freq cat a 2 cat b 1 답변 value_counts()를 사용하십시오. @DSM이 의견을 남겼습니다. In [37]: df = pd.DataFrame({'a':list('abssbab')}) df['a'].value_counts() Out[37]: b 3 a 2 s 2 dtype: int64 또한 groupby와 count도 있습니다. 여기에는 여러 가지 방법이 있습니다. In [38]: df.groupby('a').count() Out[38]: a a a 2 b 3 s 2 [3 rows x 1 columns] 온라인.. 2023. 10. 25.
Python 파이썬에서 날짜 범위를 반복하는 것, Iterating through a range of dates in Python 질문 다음과 같은 코드가 있습니다. 그러나 어떻게 더 좋게 할 수 있을까요? 현재는 중첩된 루프보다는 더 좋다고 생각하지만, 생성기가 목록 표현식에 포함되면 Perl-one-linerish해집니다. day_count = (end_date - start_date).days + 1 for single_date in [d for d in (start_date + timedelta(n) for n in range(day_count)) if d 2023. 10. 9.
Python 스레드에서 반환 값을 어떻게 얻을 수 있나요?, How to get the return value from a thread? 질문 아래의 함수 foo는 문자열 'foo'를 반환합니다. 스레드의 대상에서 반환된 'foo' 값을 어떻게 얻을 수 있을까요? from threading import Thread def foo(bar): print('hello {}'.format(bar)) return 'foo' thread = Thread(target=foo, args=('world!',)) thread.start() return_value = thread.join() 위에서 보여진 "하나의 명백한 방법"은 작동하지 않습니다: thread.join()은 None을 반환했습니다. 답변 하나의 방법은 가변 객체(예: 리스트 또는 사전)를 스레드의 생성자와 함께 전달하여 스레드가 해당 객체의 전용 슬롯에 결과를 저장하는 것입니다. 예를 들면.. 2023. 9. 10.