Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

python - Loop through multiple DataFrame inside for loop with same calculations and append it to empty DataFrame

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can insert the 4 empty data frames in a list and then iterate on it inside the for loop. For instance:

df1 = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
df4 = pd.DataFrame()
df_l = [df1, df2, df3, df4]
filename = ['dog','cat','horse','mouse']

for i in filename:
    sub_df1 = ...
    sub_df2 = ...
    sub_df3 = ...
    sub_df4 = ...
    
    sub_df_l = [sub_df1,sub_df2,sub_df3, sub_df4]
    
    for i, df in enumerate(df_l):
        df_l[i] = df_l[i].append(sub_df_l[i].sum(), ignore_index = True)

Note that also the 4 sub dataframes are inside a list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...