0

below shown the sales data

enter image description here

i have created function so that i get the summary of sales city wise,

import pandas as pd
Super=pd.read_excel(r"C:\Script Testing\supermarket_sales.xlsx")

Location=Super[Super["City"]=="Yangon"]
def Summary():
    PDT=Location.groupby(["Product line","Invoice ID"])["Total"].sum()
    PDTT=PDT.reset_index()
    return PDT

can anyone suggest how to run this function across all the cities in the data without creating a separate function for each city

0

1 Answer 1

1

Following your own approach, you can add city as a variable to the function and call the function inside a loop:

def Summary(city):
    Location=Super[Super["City"] == city]
    PDT=Location.groupby(["Product line","Invoice ID"])["Total"].sum()
    PDTT=PDT.reset_index()
    return PDT

for city in Super['City'].unique():
    PDT = Summary(city)
    #whatever you do with PDT
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.