Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try with apply:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading