简体   繁体   中英

How can I create a new pandas DataFrame out of an existing one applying a function to every column without a for loop?

A simplified script I have now working is as follows:

columns = df.columns.tolist()

df1=pd.DataFrame()

for i in columns:
    df1[i]=[random.uniform(-1*(df[i].std()*3),(df[i].std()*3))+df[i].mean()]

How can I get the same result (a one row dataframe) with a simpler, more efficient code?

Try with apply :

df1 = df.apply(lambda x: random.uniform(-3*x.std(),3*x.std())+x.mean())

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