I would like to write mean values from one dataframe (df1) to another (dfmaster ). Manually i can manage it, but i would like to automate the process in that way, that it will be read all the columns names from the df1 (as variable) and those variable will be used in the code below, to calculate mean of all columns from the dataframe (df1).
From this dataframe should be the mean of columns calculated:
import pandas as pd
data = [[6.2, 10, 8], [6.4, 15, 13], [6.6, 14, 6]]
df1 = pd.DataFrame(data, columns = ['Prozess233', 'Prozess234', 'Prozess235'])
df1
This is the master dataframe where the means should be stored:
data = [['Prozess233','NaN', 'NaN','NaN'], ['Prozess234','NaN', 'NaN', 'NaN'], ['Prozess235','NaN', 'NaN', 'NaN']]
dfmaster = pd.DataFrame(data, columns = ['Process', 'Mean', 'St.Dev', 'Max'])
dfmaster
Here is the code to calculate the mean from one column of df1 and store the value in the master dataframe:
index = dfmaster.loc[dfmaster['Process'] == 'Prozess233'].index[0]
keep_col = ['Prozess233']
df1 = df1[keep_col]
df1 = df1[df1['Prozess233'].notna()]
meanPR = df1["Prozess233"].mean()
dfmaster.at[index, 'Mean'] = meanPR
This is the result:
I would like to have a loop, that the code above (to store the mean into the master dataframe) will perform for all the columns of the dataframe df1 - the mean for the "Prozess234" and for the "Prozess235".
I couldn`t figure out, how to store the column names (df1), in order to use them in loop. I am not sure, if this could be right approach.



df1.columns.to_list()will help you get column names to iterate/store. Checkdf1.mean()as well.