'경로 이름'과 '날짜'라는 두 개의 열이있는 데이터 프레임이 있습니다. 경로 이름에는 여러 도시의 이름이 포함되며 '날짜'는 해당 도시에서 기록 된 이벤트를 나타냅니다.
예를 들어 런던의 필터링 된 df는 다음과 같습니다. =>
Route Name Date
London 2019-10-09
London 2019-10-09
London 2019-10-10
London 2019-10-10
London 2019-10-11
London 2019-10-11
London 2019-10-11
London 2019-10-11
London 2019-10-12
London 2019-10-12
London 2019-10-12
도시 당 일수는 어떻게 알 수 있습니까?. 예상 답변은 다음과 같습니다.
Route Name Frequency(days)
London 4
Manchester 5
Glasgow 5
기타..
Quang Hoang의 의견에서 수정되었으며 월별 개수에 대한 질문을 포함하도록 수정되었습니다.
import pandas as pd
from io import StringIO
string = """Route Name\tDate
London\t2019-10-09
London\t2019-10-09
London\t2019-10-10
London\t2019-10-10
London\t2019-10-11
London\t2019-10-11
London\t2019-10-11
London\t2019-10-11
London\t2019-10-12
London\t2019-10-12
London\t2019-10-12
"""
df = pd.read_csv(StringIO(string), sep = "\t")
df["Date"] = pd.to_datetime(df["Date"], format = "%Y-%m-%d")
print(df.head())
"""
Route Name Date
0 London 2019-10-09
1 London 2019-10-09
2 London 2019-10-10
3 London 2019-10-10
4 London 2019-10-11
"""
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
print(df.head())
"""
Route Name Date Year Month
0 London 2019-10-09 2019 10
1 London 2019-10-09 2019 10
2 London 2019-10-10 2019 10
3 London 2019-10-10 2019 10
4 London 2019-10-11 2019 10
"""
annual_df = df.groupby(["Route Name", "Year"])["Date"].nunique()
print(annual_df.head())
"""
Route Name Year
London 2019 4
Name: Date, dtype: int64
"""
monthly_df = df.groupby(["Route Name", "Year", "Month"])["Date"].nunique()
print(monthly_df.head())
"""
Route Name Year Month
London 2019 10 4
Name: Date, dtype: int64
"""
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다