beginner python question here that I've had struggles getting answered from related stack questions.
I've got a list
dfList = df0,df1,df2,...,df7
I've got a function that I've defined and takes a dataframe as its argument. I'm not sure the function itself matters, but to be safe it is basically
def rateCalc (outcomeDataFrame):
rateList = list()
upperRateList = list()
lowerRateList = list()
for i in range(len(outcomeDataFrame)):
lowlevel, highlevel = proportion_confint(count=outcomeDataFrame.iloc[i,4], nobs=outcomeDataFrame.iloc[i,3])
lowerRateList.append(lowlevel)
rateList.append(outcomeDataFrame.iloc[i,4]/outcomeDataFrame.iloc[i,3])
upperRateList.append(highlevel)
outcomeDataFrame = outcomeDataFrame.assign(lowerRate=lowerRateList)
outcomeDataFrame = outcomeDataFrame.assign(midrate=rateList)
outcomeDataFrame = outcomeDataFrame.assign(upperRate=upperRateList)
return outcomeDataFrame
What I'm trying to do is append a the observed success ratio of two numbers as well as their 95% confidence interval. Goes fine when working with any individual df.
What I want to accomplish is turn each item of dfList into a version of itself with those lowerRate, midRate, and higherRate values appended as new columns.
When I try to apply across each dataframe with
for i in range(len(dfList):
rateCalc(dfList[i])
though, it seems to only execute for df0. I can't make any sense of that; a full error I'd assume I had some basic flaw in the code, but it seems to work for df0 and then not iterate to df1 and beyond.
I also thought there may be an issue of "df1 != dfList[1]" in some backend sense (that running the function on the item in a list dfList[1] would not have any affect on the original item df1) but, again, the fact it seems to work with df0 would imply that's not the issue.
I also tried throwing some mud at the wall with the "map" function but am not sure I understand how to use that in this context (or any other for that matter ha)
Thanks all
dfdoesn't exist in your example. Perhaps you wantfor df in dfList: rateCalc(df).print(i, id(dfList[i]))to make sure that list is setup properly.proportion_confintfunction complicated? Can you post an example?for i in range(len(dfList)): dfList[i] = rateCalc(dfList[i])is what you want. That doesn't explain only processing df0, but it does explain why none of them update.