Hướng dẫn how to group zip codes in python - cách nhóm mã zip trong python

Tôi có một khung dữ liệu lớn với dữ liệu giao dịch. Những gì tôi đang cố gắng làm là sử dụng Python để tổng hợp dữ liệu bắt đầu bằng mã zip, sau đó là một năm và tháng, cuối cùng là tổng số giao dịch cho tháng đó.

Show

    DF của tôi:

      Date        VAR1   VAR2    ZipCode    Transactions
    YYYY-MM-DD.    X.     Y.     12345.         1.      
    

    Vì vậy, điều đầu tiên tôi làm là chuyển đổi thời gian đến nay

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    

    Sau đó, tôi chia dữ liệu thành tháng và số lượng giao dịch:

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    

    Trong đó cung cấp đầu ra của:

    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    

    Câu hỏi của tôi là, tôi còn thiếu gì để có được mã zip ở phía trước:

    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    

    Bất kỳ và tất cả sự giúp đỡ đều được đánh giá cao

    Thực hiện đầy đủ:

    Xem thảo luận

    Cải thiện bài viết

  • Lưu bài viết
  • Đọc
  • Thực hiện đầy đủ:

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viếtGeopy module.geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the world.

    Để cài đặt mô -đun Geopy, hãy chạy lệnh sau trong thiết bị đầu cuối của bạn.

    pip install geopy
    

    Approach:

    • Nhập mô -đun địa lý
    • Khởi tạo API nominatim để nhận vị trí từ chuỗi đầu vào.
    • Nhận vị trí với hàm golocator.geocode ().
    • Bây giờ trích xuất dữ liệu zip từ thể hiện vị trí.

    Thực hiện từng bước:

    Bước #1: Nhập mô -đun.Import module.

    Python3

    from geopy.geocoders import Nominatim

    Bước #2: Tạo một đối tượng Nominatim và khởi tạo API Nominatim & NBSP; với tham số Geoapiexercise.Make a Nominatim object and initialize Nominatim API  with the geoapiExercises parameter.

    Python3

    ____10

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    2
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    4
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    5

    Bước #3: & nbsp; bây giờ nhận được địa chỉ đầy đủ với Geocode ().Now get a complete address with geocode().

    Python3

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    6
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    8

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    9
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    1

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    3

    Output:

    Đường nhàm chán, Digha, Patna, Patna Nông thôn, Patna, Bihar, 800001, Ấn Độ

    Bước #4: Bây giờ hãy lấy thông tin từ danh sách đã cho và được phân tích cú pháp vào từ điển có chức năng thô ().Now get the information from the given list and parsed into a dictionary with raw function().

    Python3

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    4
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    6

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    8

    Output:

    Hướng dẫn how to group zip codes in python - cách nhóm mã zip trong python

    Bước #5: Bây giờ đi qua mã zip từ một từ điển nhất định.Now traverse the ZIP Code from a given dictionary.

    Python3

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    9
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    1
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    3

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    5
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    6
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    5

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    9

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    5
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    2
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    3
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    4
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    5
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    6

    Output:

    Hướng dẫn how to group zip codes in python - cách nhóm mã zip trong python

    Thực hiện đầy đủ:

    Python3

    from geopy.geocoders import Nominatim

    ____10

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    2
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    4
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    5

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    6
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    8

     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    9
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    1

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    4
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    6

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    9
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    1
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    1
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    3

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    5
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    6
     df['Date'] = pd.to_datetime(df['Date'])
     df.info()
     # Date datetime64[ns]
    
    5

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    9

    # grouping the data by year and month
    per = df.Date.dt.to_period("M")  
    g = df.groupby(per)
    g.sum() # so now that this works, we need to break it up into zip codes
    
    2
    Date.       Transactions
    YYYY-MM.        X
    YYYY-MM.        Y
    
    5
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    2
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    3
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    4
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    5
    ZipCode.     Date.    Transactions
     123345.   YYYY-MM.     sum()
    
    6

    Output:

    Hướng dẫn how to group zip codes in python - cách nhóm mã zip trong python