I am struggling to get a solution for this.
So I have 4 empty DataFrame
df1 = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
df4 = pd.DataFrame()
a list of filenames
filename = ['dog','cat','horse','mouse']
I am looping through every file in the list. For the first file after some calculations, 4 new DataFrame is made
for i in filename:
f = open(f'{i}.json', "r")
f = json.loads(f.read())
#######
some calculations
#######
subdf1
subdf2
subdf3
subdf4
now the main problem I am struggling with is, I have some new calculations to be done on all 4 subdf's with which are all same calculations and append to the empty Dataframe above accordingly.
for example calculation like:
i = subdf1.sum()
df1 = df1.append(i)
What I am doing right now is repeating the same calculations for all subdf's like below:
i = subdf1.sum()
df1 = df1.append(i)
i = subdf2.sum()
df2 = df2.append(i)
i = subdf3.sum()
df3 = df3.append(i)
i = subdf4.sum()
df4 = df4.append(i)
Full code of I have been doing till now
df1 = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
df4 = pd.DataFrame()
filename = ['dog','cat','horse','mouse']
for i in filename:
f = open(f'{i}.json', "r")
f = json.loads(f.read())
#######
some calculations
#######
subdf1
subdf2
subdf3
subdf4
i = subdf1.sum()
df1 = df1.append(i)
i = subdf2.sum()
df2 = df2.append(i)
i = subdf3.sum()
df3 = df3.append(i)
i = subdf4.sum()
df4 = df4.append(i)
What I want to achieve is to do the same calculations for all 4 subdf's and append them accordingly to their empty DataFrame. in short I want the last code to make short.
Thanks in advance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…