简体   繁体   中英

Iterating through multiple pandas Dataframe object

I have a forecasting function which I have applied to one dataframe and it works fine.

output =forecast(rawdf)

the question is I can extract muliple dataframe and if I want to use the same function to run over all the dataframe. Because rawdf have a column named DepotName which have multiple unique values.

My aim is to break down the rawdf into multiple dataframes corresponding to the depot and apply the forecast function to all of them individually.

DepotList= ['A', 'B']

for Depot in DepotList:
    i=1
    rawdf=rawdf.loc[rawdf.DepotName.isin([DepotList])]
    output[i] =forecast(rawdf)
    i = i+1

I havent tried it , as if I am gonna do this for first time. I will appreciate your time and effort to help me accomplish this mission.

Try the same logic with pass DepotList value one by one.

instead of passing list in isin() , pass value by iterating list

DepotList= ['A', 'B']
result = []
for Depot in DepotList:

    resultByDepotName =rawdf.loc[rawdf.DepotName.isin(Depot)]
    list = forecast(resultByDepotName)
    result.append(list)

result will contains forecast values

Dont't reassign same dataframe variable of result rawdf.loc[rawdf.DepotName.isin(Depot)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM